
<!DOCTYPE HTML>
<html lang="zh-hans" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>10.4 安装超级构建 · 《CMake Cookbook》中文版</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        <meta name="author" content="陈晓伟 chenxiaowei">
        
        
    
    <link rel="stylesheet" href="../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-search-pro/search.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-pageview-count/plugin.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="../chapter11/11.0-chinese.html" />
    
    
    <link rel="prev" href="10.3-chinese.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="输入并搜索" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../../">
            
                <a href="../../">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../preface/preface-chinese.html">
            
                <a href="../preface/preface-chinese.html">
            
                    
                    前言
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../chapter0/0.0-chinese.html">
            
                <a href="../chapter0/0.0-chinese.html">
            
                    
                    第0章 配置环境
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="../chapter0/0.1-chinese.html">
            
                <a href="../chapter0/0.1-chinese.html">
            
                    
                    0.1 获取代码
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="../chapter0/0.2-chinese.html">
            
                <a href="../chapter0/0.2-chinese.html">
            
                    
                    0.2 Docker镜像
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="../chapter0/0.3-chinese.html">
            
                <a href="../chapter0/0.3-chinese.html">
            
                    
                    0.3 安装必要的软件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.4" data-path="../chapter0/0.4-chinese.html">
            
                <a href="../chapter0/0.4-chinese.html">
            
                    
                    0.4 测试环境
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.5" data-path="../chapter0/0.5-chinese.html">
            
                <a href="../chapter0/0.5-chinese.html">
            
                    
                    0.5 上报问题并提出改进建议
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="../chapter1/1.0-chinese.html">
            
                <a href="../chapter1/1.0-chinese.html">
            
                    
                    第1章 从可执行文件到库
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="../chapter1/1.1-chinese.html">
            
                <a href="../chapter1/1.1-chinese.html">
            
                    
                    1.1 将单个源文件编译为可执行文件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="../chapter1/1.2-chinese.html">
            
                <a href="../chapter1/1.2-chinese.html">
            
                    
                    1.2 切换生成器
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="../chapter1/1.3-chinese.html">
            
                <a href="../chapter1/1.3-chinese.html">
            
                    
                    1.3 构建和链接静态库和动态库
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.4" data-path="../chapter1/1.4-chinese.html">
            
                <a href="../chapter1/1.4-chinese.html">
            
                    
                    1.4 用条件句控制编译
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.5" data-path="../chapter1/1.5-chinese.html">
            
                <a href="../chapter1/1.5-chinese.html">
            
                    
                    1.5 向用户显示选项
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.6" data-path="../chapter1/1.6-chinese.html">
            
                <a href="../chapter1/1.6-chinese.html">
            
                    
                    1.6 指定编译器
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.7" data-path="../chapter1/1.7-chinese.html">
            
                <a href="../chapter1/1.7-chinese.html">
            
                    
                    1.7 切换构建类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.8" data-path="../chapter1/1.8-chinese.html">
            
                <a href="../chapter1/1.8-chinese.html">
            
                    
                    1.8 设置编译器选项
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.9" data-path="../chapter1/1.9-chinese.html">
            
                <a href="../chapter1/1.9-chinese.html">
            
                    
                    1.9 为语言设定标准
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.10" data-path="../chapter1/1.10-chinese.html">
            
                <a href="../chapter1/1.10-chinese.html">
            
                    
                    1.10 使用控制流
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="../chapter2/2.0-chinese.html">
            
                <a href="../chapter2/2.0-chinese.html">
            
                    
                    第2章 检测环境
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="../chapter2/2.1-chinese.html">
            
                <a href="../chapter2/2.1-chinese.html">
            
                    
                    2.1 检测操作系统
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2" data-path="../chapter2/2.2-chinese.html">
            
                <a href="../chapter2/2.2-chinese.html">
            
                    
                    2.2 处理与平台相关的源代码
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.3" data-path="../chapter2/2.3-chinese.html">
            
                <a href="../chapter2/2.3-chinese.html">
            
                    
                    2.3 处理与编译器相关的源代码
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.4" data-path="../chapter2/2.4-chinese.html">
            
                <a href="../chapter2/2.4-chinese.html">
            
                    
                    2.4 检测处理器体系结构
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.5" data-path="../chapter2/2.5-chinese.html">
            
                <a href="../chapter2/2.5-chinese.html">
            
                    
                    2.5 检测处理器指令集
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.6" data-path="../chapter2/2.6-chinese.html">
            
                <a href="../chapter2/2.6-chinese.html">
            
                    
                    2.6 为Eigen库使能向量化
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="../chapter3/3.0-chinese.html">
            
                <a href="../chapter3/3.0-chinese.html">
            
                    
                    第3章 检测外部库和程序
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.6.1" data-path="../chapter3/3.1-chinese.html">
            
                <a href="../chapter3/3.1-chinese.html">
            
                    
                    3.1 检测Python解释器
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.2" data-path="../chapter3/3.2-chinese.html">
            
                <a href="../chapter3/3.2-chinese.html">
            
                    
                    3.2 检测Python库
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.3" data-path="../chapter3/3.3-chinese.html">
            
                <a href="../chapter3/3.3-chinese.html">
            
                    
                    3.3 检测Python模块和包
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.4" data-path="../chapter3/3.4-chinese.html">
            
                <a href="../chapter3/3.4-chinese.html">
            
                    
                    3.4 检测BLAS和LAPACK数学库
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.5" data-path="../chapter3/3.5-chinese.html">
            
                <a href="../chapter3/3.5-chinese.html">
            
                    
                    3.5 检测OpenMP的并行环境
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.6" data-path="../chapter3/3.6-chinese.html">
            
                <a href="../chapter3/3.6-chinese.html">
            
                    
                    3.6 检测MPI的并行环境
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.7" data-path="../chapter3/3.7-chinese.html">
            
                <a href="../chapter3/3.7-chinese.html">
            
                    
                    3.7 检测Eigen库
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.8" data-path="../chapter3/3.8-chinese.html">
            
                <a href="../chapter3/3.8-chinese.html">
            
                    
                    3.8 检测Boost库
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.9" data-path="../chapter3/3.9-chinese.html">
            
                <a href="../chapter3/3.9-chinese.html">
            
                    
                    3.9 检测外部库:Ⅰ. 使用pkg-config
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.10" data-path="../chapter3/3.10-chinese.html">
            
                <a href="../chapter3/3.10-chinese.html">
            
                    
                    3.10 检测外部库:Ⅱ. 自定义find模块
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="../chapter4/4.0-chinese.html">
            
                <a href="../chapter4/4.0-chinese.html">
            
                    
                    第4章 创建和运行测试
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.7.1" data-path="../chapter4/4.1-chinese.html">
            
                <a href="../chapter4/4.1-chinese.html">
            
                    
                    4.1 创建一个简单的单元测试
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.2" data-path="../chapter4/4.2-chinese.html">
            
                <a href="../chapter4/4.2-chinese.html">
            
                    
                    4.2 使用Catch2库进行单元测试
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.3" data-path="../chapter4/4.3-chinese.html">
            
                <a href="../chapter4/4.3-chinese.html">
            
                    
                    4.3 使用Google Test库进行单元测试
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.4" data-path="../chapter4/4.4-chinese.html">
            
                <a href="../chapter4/4.4-chinese.html">
            
                    
                    4.4 使用Boost Test进行单元测试
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.5" data-path="../chapter4/4.5-chinese.html">
            
                <a href="../chapter4/4.5-chinese.html">
            
                    
                    4.5 使用动态分析来检测内存缺陷
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.6" data-path="../chapter4/4.6-chinese.html">
            
                <a href="../chapter4/4.6-chinese.html">
            
                    
                    4.6 预期测试失败
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.7" data-path="../chapter4/4.7-chinese.html">
            
                <a href="../chapter4/4.7-chinese.html">
            
                    
                    4.7 使用超时测试运行时间过长的测试
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.8" data-path="../chapter4/4.8-chinese.html">
            
                <a href="../chapter4/4.8-chinese.html">
            
                    
                    4.8 并行测试
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.9" data-path="../chapter4/4.9-chinese.html">
            
                <a href="../chapter4/4.9-chinese.html">
            
                    
                    4.9 运行测试子集
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.10" data-path="../chapter4/4.10-chinese.html">
            
                <a href="../chapter4/4.10-chinese.html">
            
                    
                    4.10 使用测试固件
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="../chapter5/5.0-chinese.html">
            
                <a href="../chapter5/5.0-chinese.html">
            
                    
                    第5章 配置时和构建时的操作
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.8.1" data-path="../chapter5/5.1-chinese.html">
            
                <a href="../chapter5/5.1-chinese.html">
            
                    
                    5.1 使用平台无关的文件操作
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.2" data-path="../chapter5/5.2-chinese.html">
            
                <a href="../chapter5/5.2-chinese.html">
            
                    
                    5.2 配置时运行自定义命令
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.3" data-path="../chapter5/5.3-chinese.html">
            
                <a href="../chapter5/5.3-chinese.html">
            
                    
                    5.3 构建时运行自定义命令:Ⅰ. 使用add_custom_command
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.4" data-path="../chapter5/5.4-chinese.html">
            
                <a href="../chapter5/5.4-chinese.html">
            
                    
                    5.4 构建时运行自定义命令:Ⅱ. 使用add_custom_target
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.5" data-path="../chapter5/5.5-chinese.html">
            
                <a href="../chapter5/5.5-chinese.html">
            
                    
                    5.5 构建时为特定目标运行自定义命令
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.6" data-path="../chapter5/5.6-chinese.html">
            
                <a href="../chapter5/5.6-chinese.html">
            
                    
                    5.6 探究编译和链接命令
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.7" data-path="../chapter5/5.7-chinese.html">
            
                <a href="../chapter5/5.7-chinese.html">
            
                    
                    5.7 探究编译器标志命令
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.8" data-path="../chapter5/5.8-chinese.html">
            
                <a href="../chapter5/5.8-chinese.html">
            
                    
                    5.8 探究可执行命令
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.9" data-path="../chapter5/5.9-chinese.html">
            
                <a href="../chapter5/5.9-chinese.html">
            
                    
                    5.9 使用生成器表达式微调配置和编译
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="../chapter6/6.0-chinese.html">
            
                <a href="../chapter6/6.0-chinese.html">
            
                    
                    第6章 生成源码
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.9.1" data-path="../chapter6/6.1-chinese.html">
            
                <a href="../chapter6/6.1-chinese.html">
            
                    
                    6.1 配置时生成源码
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.2" data-path="../chapter6/6.2-chinese.html">
            
                <a href="../chapter6/6.2-chinese.html">
            
                    
                    6.2 使用Python在配置时生成源码
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.3" data-path="../chapter6/6.3-chinese.html">
            
                <a href="../chapter6/6.3-chinese.html">
            
                    
                    6.3 构建时使用Python生成源码
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.4" data-path="../chapter6/6.4-chinese.html">
            
                <a href="../chapter6/6.4-chinese.html">
            
                    
                    6.4 记录项目版本信息以便报告
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.5" data-path="../chapter6/6.5-chinese.html">
            
                <a href="../chapter6/6.5-chinese.html">
            
                    
                    6.5 从文件中记录项目版本
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.6" data-path="../chapter6/6.6-chinese.html">
            
                <a href="../chapter6/6.6-chinese.html">
            
                    
                    6.6 配置时记录Git Hash值
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.7" data-path="../chapter6/6.7-chinese.html">
            
                <a href="../chapter6/6.7-chinese.html">
            
                    
                    6.7 构建时记录Git Hash值
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="../chapter7/7.0-chinese.html">
            
                <a href="../chapter7/7.0-chinese.html">
            
                    
                    第7章 构建项目
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.10.1" data-path="../chapter7/7.1-chinese.html">
            
                <a href="../chapter7/7.1-chinese.html">
            
                    
                    7.1 使用函数和宏重用代码
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.2" data-path="../chapter7/7.2-chinese.html">
            
                <a href="../chapter7/7.2-chinese.html">
            
                    
                    7.2 将CMake源代码分成模块
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.3" data-path="../chapter7/7.3-chinese.html">
            
                <a href="../chapter7/7.3-chinese.html">
            
                    
                    7.3 编写函数来测试和设置编译器标志
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.4" data-path="../chapter7/7.4-chinese.html">
            
                <a href="../chapter7/7.4-chinese.html">
            
                    
                    7.4 用指定参数定义函数或宏
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.5" data-path="../chapter7/7.5-chinese.html">
            
                <a href="../chapter7/7.5-chinese.html">
            
                    
                    7.5 重新定义函数和宏
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.6" data-path="../chapter7/7.6-chinese.html">
            
                <a href="../chapter7/7.6-chinese.html">
            
                    
                    7.6 使用废弃函数、宏和变量
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.7" data-path="../chapter7/7.7-chinese.html">
            
                <a href="../chapter7/7.7-chinese.html">
            
                    
                    7.7 add_subdirectory的限定范围
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.8" data-path="../chapter7/7.8-chinese.html">
            
                <a href="../chapter7/7.8-chinese.html">
            
                    
                    7.8 使用target_sources避免全局变量
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.9" data-path="../chapter7/7.9-chinese.html">
            
                <a href="../chapter7/7.9-chinese.html">
            
                    
                    7.9 组织Fortran项目
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.11" data-path="../chapter8/8.0-chinese.html">
            
                <a href="../chapter8/8.0-chinese.html">
            
                    
                    第8章 超级构建模式
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.11.1" data-path="../chapter8/8.1-chinese.html">
            
                <a href="../chapter8/8.1-chinese.html">
            
                    
                    8.1 使用超级构建模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11.2" data-path="../chapter8/8.2-chinese.html">
            
                <a href="../chapter8/8.2-chinese.html">
            
                    
                    8.2 使用超级构建管理依赖项:Ⅰ.Boost库
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11.3" data-path="../chapter8/8.3-chinese.html">
            
                <a href="../chapter8/8.3-chinese.html">
            
                    
                    8.3 使用超级构建管理依赖项:Ⅱ.FFTW库
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11.4" data-path="../chapter8/8.4-chinese.html">
            
                <a href="../chapter8/8.4-chinese.html">
            
                    
                    8.4 使用超级构建管理依赖项:Ⅲ.Google Test框架
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11.5" data-path="../chapter8/8.5-chinese.html">
            
                <a href="../chapter8/8.5-chinese.html">
            
                    
                    8.5 使用超级构建支持项目
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.12" data-path="../chapter9/9.0-chinese.html">
            
                <a href="../chapter9/9.0-chinese.html">
            
                    
                    第9章 语言混合项目
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.12.1" data-path="../chapter9/9.1-chinese.html">
            
                <a href="../chapter9/9.1-chinese.html">
            
                    
                    9.1 使用C/C++库构建Fortran项目
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12.2" data-path="../chapter9/9.2-chinese.html">
            
                <a href="../chapter9/9.2-chinese.html">
            
                    
                    9.2 使用Fortran库构建C/C++项目
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12.3" data-path="../chapter9/9.3-chinese.html">
            
                <a href="../chapter9/9.3-chinese.html">
            
                    
                    9.3 使用Cython构建C++和Python项目
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12.4" data-path="../chapter9/9.4-chinese.html">
            
                <a href="../chapter9/9.4-chinese.html">
            
                    
                    9.4 使用Boost.Python构建C++和Python项目
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12.5" data-path="../chapter9/9.5-chinese.html">
            
                <a href="../chapter9/9.5-chinese.html">
            
                    
                    9.5 使用pybind11构建C++和Python项目
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12.6" data-path="../chapter9/9.6-chinese.html">
            
                <a href="../chapter9/9.6-chinese.html">
            
                    
                    9.6 使用Python CFFI混合C，C++，Fortran和Python
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.13" data-path="10.0-chinese.html">
            
                <a href="10.0-chinese.html">
            
                    
                    第10章 编写安装程序
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.13.1" data-path="10.1-chinese.html">
            
                <a href="10.1-chinese.html">
            
                    
                    10.1 安装项目
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13.2" data-path="10.2-chinese.html">
            
                <a href="10.2-chinese.html">
            
                    
                    10.2 生成输出头文件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13.3" data-path="10.3-chinese.html">
            
                <a href="10.3-chinese.html">
            
                    
                    10.3 输出目标
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.13.4" data-path="10.4-chinese.html">
            
                <a href="10.4-chinese.html">
            
                    
                    10.4 安装超级构建
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.14" data-path="../chapter11/11.0-chinese.html">
            
                <a href="../chapter11/11.0-chinese.html">
            
                    
                    第11章 打包项目
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.14.1" data-path="../chapter11/11.1-chinese.html">
            
                <a href="../chapter11/11.1-chinese.html">
            
                    
                    11.1 生成源代码和二进制包
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14.2" data-path="../chapter11/11.2-chinese.html">
            
                <a href="../chapter11/11.2-chinese.html">
            
                    
                    11.2 通过PyPI发布使用CMake/pybind11构建的C++/Python项目
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14.3" data-path="../chapter11/11.3-chinese.html">
            
                <a href="../chapter11/11.3-chinese.html">
            
                    
                    11.3 通过PyPI发布使用CMake/CFFI构建C/Fortran/Python项目
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14.4" data-path="../chapter11/11.4-chinese.html">
            
                <a href="../chapter11/11.4-chinese.html">
            
                    
                    11.4 以Conda包的形式发布一个简单的项目
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14.5" data-path="../chapter11/11.5-chinese.html">
            
                <a href="../chapter11/11.5-chinese.html">
            
                    
                    11.5 将Conda包作为依赖项发布给项目
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.15" data-path="../chapter12/12.0-chinese.html">
            
                <a href="../chapter12/12.0-chinese.html">
            
                    
                    第12章 构建文档
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.15.1" data-path="../chapter12/12.1-chinese.html">
            
                <a href="../chapter12/12.1-chinese.html">
            
                    
                    12.1 使用Doxygen构建文档
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.15.2" data-path="../chapter12/12.2-chinese.html">
            
                <a href="../chapter12/12.2-chinese.html">
            
                    
                    12.2 使用Sphinx构建文档
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.15.3" data-path="../chapter12/12.3-chinese.html">
            
                <a href="../chapter12/12.3-chinese.html">
            
                    
                    12.3 结合Doxygen和Sphinx
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.16" data-path="../chapter13/13.0-chinese.html">
            
                <a href="../chapter13/13.0-chinese.html">
            
                    
                    第13章 选择生成器和交叉编译
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.16.1" data-path="../chapter13/13.1-chinese.html">
            
                <a href="../chapter13/13.1-chinese.html">
            
                    
                    13.1 使用CMake构建Visual Studio 2017项目
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.16.2" data-path="../chapter13/13.2-chinese.html">
            
                <a href="../chapter13/13.2-chinese.html">
            
                    
                    13.2 交叉编译hello world示例
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.16.3" data-path="../chapter13/13.3-chinese.html">
            
                <a href="../chapter13/13.3-chinese.html">
            
                    
                    13.3 使用OpenMP并行化交叉编译Windows二进制文件
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.17" data-path="../chapter14/14.0-chinese.html">
            
                <a href="../chapter14/14.0-chinese.html">
            
                    
                    第14章 测试面板
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.17.1" data-path="../chapter14/14.1-chinese.html">
            
                <a href="../chapter14/14.1-chinese.html">
            
                    
                    14.1 将测试部署到CDash
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.17.2" data-path="../chapter14/14.2-chinese.html">
            
                <a href="../chapter14/14.2-chinese.html">
            
                    
                    14.2 CDash显示测试覆盖率
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.17.3" data-path="../chapter14/14.3-chinese.html">
            
                <a href="../chapter14/14.3-chinese.html">
            
                    
                    14.3 使用AddressSanifier向CDash报告内存缺陷
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.17.4" data-path="../chapter14/14.4-chinese.html">
            
                <a href="../chapter14/14.4-chinese.html">
            
                    
                    14.4 使用ThreadSaniiser向CDash报告数据争用
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.18" data-path="../chapter15/15.0-chinese.html">
            
                <a href="../chapter15/15.0-chinese.html">
            
                    
                    第15章 使用CMake构建已有项目
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.18.1" data-path="../chapter15/15.1-chinese.html">
            
                <a href="../chapter15/15.1-chinese.html">
            
                    
                    15.1 如何开始迁移项目
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.18.2" data-path="../chapter15/15.2-chinese.html">
            
                <a href="../chapter15/15.2-chinese.html">
            
                    
                    15.2 生成文件并编写平台检查
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.18.3" data-path="../chapter15/15.3-chinese.html">
            
                <a href="../chapter15/15.3-chinese.html">
            
                    
                    15.3 检测所需的链接和依赖关系
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.18.4" data-path="../chapter15/15.4-chinese.html">
            
                <a href="../chapter15/15.4-chinese.html">
            
                    
                    15.4 复制编译标志
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.18.5" data-path="../chapter15/15.5-chinese.html">
            
                <a href="../chapter15/15.5-chinese.html">
            
                    
                    15.5 移植测试
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.18.6" data-path="../chapter15/15.6-chinese.html">
            
                <a href="../chapter15/15.6-chinese.html">
            
                    
                    15.6 移植安装目标
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.18.7" data-path="../chapter15/15.7-chinese.html">
            
                <a href="../chapter15/15.7-chinese.html">
            
                    
                    15.7 进一步迁移的措施
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.18.8" data-path="../chapter15/15.8-chinese.html">
            
                <a href="../chapter15/15.8-chinese.html">
            
                    
                    15.8 项目转换为CMake的常见问题
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.19" data-path="../chapter16/16.0-chinese.html">
            
                <a href="../chapter16/16.0-chinese.html">
            
                    
                    第16章 可能感兴趣的书
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.19.1" data-path="../chapter16/16.1-chinese.html">
            
                <a href="../chapter16/16.1-chinese.html">
            
                    
                    16.1 留下评论——让其他读者知道你的想法
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="http://www.bczl.xyz/" target="blank" class="gitbook-link">
            编程之路
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../.." >10.4 安装超级构建</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="104-&#x5B89;&#x88C5;&#x8D85;&#x7EA7;&#x6784;&#x5EFA;">10.4 &#x5B89;&#x88C5;&#x8D85;&#x7EA7;&#x6784;&#x5EFA;</h1>
<p><strong>NOTE</strong>:<em>&#x6B64;&#x793A;&#x4F8B;&#x4EE3;&#x7801;&#x53EF;&#x4EE5;&#x5728; <a href="https://github.com/dev-cafe/cmake-cookbook/tree/v1.0/chapter-10/recipe-04" target="_blank">https://github.com/dev-cafe/cmake-cookbook/tree/v1.0/chapter-10/recipe-04</a> &#x4E2D;&#x627E;&#x5230;&#xFF0C;&#x5176;&#x4E2D;&#x6709;&#x4E00;&#x4E2A;C++&#x793A;&#x4F8B;&#x3002;&#x8BE5;&#x793A;&#x4F8B;&#x5728;CMake 3.6&#x7248;(&#x6216;&#x66F4;&#x9AD8;&#x7248;&#x672C;)&#x4E2D;&#x662F;&#x6709;&#x6548;&#x7684;&#xFF0C;&#x5E76;&#x4E14;&#x5DF2;&#x7ECF;&#x5728;GNU/Linux&#x3001;macOS&#x548C;Windows&#x4E0A;&#x8FDB;&#x884C;&#x8FC7;&#x6D4B;&#x8BD5;&#x3002;</em></p>
<p>&#x6211;&#x4EEC;&#x7684;&#x6D88;&#x606F;&#x5E93;&#x53D6;&#x5F97;&#x4E86;&#x5DE8;&#x5927;&#x7684;&#x6210;&#x529F;&#xFF0C;&#x8BB8;&#x591A;&#x5176;&#x4ED6;&#x7A0B;&#x5E8F;&#x5458;&#x90FD;&#x4F7F;&#x7528;&#x5B83;&#xFF0C;&#x5E76;&#x4E14;&#x975E;&#x5E38;&#x6EE1;&#x610F;&#x3002;&#x4E5F;&#x5E0C;&#x671B;&#x5728;&#x81EA;&#x5DF1;&#x7684;&#x9879;&#x76EE;&#x4E2D;&#x4F7F;&#x7528;&#x5B83;&#xFF0C;&#x4F46;&#x662F;&#x4E0D;&#x786E;&#x5B9A;&#x5982;&#x4F55;&#x6B63;&#x786E;&#x5730;&#x7BA1;&#x7406;&#x4F9D;&#x8D56;&#x5173;&#x7CFB;&#x3002;&#x53EF;&#x4EE5;&#x7528;&#x81EA;&#x5DF1;&#x7684;&#x4EE3;&#x7801;&#x9644;&#x5E26;&#x6D88;&#x606F;&#x5E93;&#x7684;&#x6E90;&#x4EE3;&#x7801;&#xFF0C;&#x4F46;&#x662F;&#x5982;&#x679C;&#x8BE5;&#x5E93;&#x5DF2;&#x7ECF;&#x5B89;&#x88C5;&#x5728;&#x7CFB;&#x7EDF;&#x4E0A;&#x4E86;&#x5E94;&#x8BE5;&#x600E;&#x4E48;&#x505A;&#x5462;&#xFF1F;&#x7B2C;8&#x7AE0;&#xFF0C;&#x5C55;&#x793A;&#x4E86;&#x8D85;&#x7EA7;&#x6784;&#x5EFA;&#x7684;&#x573A;&#x666F;&#xFF0C;&#x4F46;&#x662F;&#x4E0D;&#x786E;&#x5B9A;&#x5982;&#x4F55;&#x5B89;&#x88C5;&#x8FD9;&#x6837;&#x7684;&#x9879;&#x76EE;&#x3002;&#x672C;&#x793A;&#x4F8B;&#x5C06;&#x5E26;&#x60A8;&#x4E86;&#x89E3;&#x5B89;&#x88C5;&#x8D85;&#x7EA7;&#x6784;&#x5EFA;&#x7684;&#x5B89;&#x88C5;&#x7EC6;&#x8282;&#x3002;</p>
<h2 id="&#x51C6;&#x5907;&#x5DE5;&#x4F5C;">&#x51C6;&#x5907;&#x5DE5;&#x4F5C;</h2>
<p>&#x6B64;&#x793A;&#x4F8B;&#x5C06;&#x9488;&#x5BF9;&#x6D88;&#x606F;&#x5E93;&#xFF0C;&#x6784;&#x5EFA;&#x4E00;&#x4E2A;&#x7B80;&#x5355;&#x7684;&#x53EF;&#x6267;&#x884C;&#x94FE;&#x63A5;&#x3002;&#x9879;&#x76EE;&#x5E03;&#x5C40;&#x5982;&#x4E0B;:</p>
<pre><code class="lang-shell">&#x251C;&#x2500;&#x2500; cmake
&#x2502;    &#x251C;&#x2500;&#x2500; install_hook.cmake.in
&#x2502;    &#x2514;&#x2500;&#x2500; print_rpath.py
&#x251C;&#x2500;&#x2500; CMakeLists.txt
&#x251C;&#x2500;&#x2500; external
&#x2502;    &#x2514;&#x2500;&#x2500; upstream
&#x2502;        &#x251C;&#x2500;&#x2500; CMakeLists.txt
&#x2502;        &#x2514;&#x2500;&#x2500; message
&#x2502;            &#x2514;&#x2500;&#x2500; CMakeLists.txt
&#x2514;&#x2500;&#x2500; src
    &#x251C;&#x2500;&#x2500; CMakeLists.txt
    &#x2514;&#x2500;&#x2500; use_message.cpp
</code></pre>
<p>&#x4E3B;<code>CMakeLists.txt</code>&#x6587;&#x4EF6;&#x914D;&#x5408;&#x8D85;&#x7EA7;&#x6784;&#x5EFA;&#xFF0C;<code>external</code>&#x5B50;&#x76EE;&#x5F55;&#x5305;&#x542B;&#x5904;&#x7406;&#x4F9D;&#x8D56;&#x9879;&#x7684;CMake&#x6307;&#x4EE4;&#x3002;<code>cmake</code>&#x5B50;&#x76EE;&#x5F55;&#x5305;&#x542B;&#x4E00;&#x4E2A;Python&#x811A;&#x672C;&#x548C;&#x4E00;&#x4E2A;&#x6A21;&#x677F;CMake&#x811A;&#x672C;&#x3002;&#x8FD9;&#x4E9B;&#x5C06;&#x7528;&#x4E8E;&#x5B89;&#x88C5;&#x65B9;&#x9762;&#x7684;&#x5FAE;&#x8C03;&#xFF0C;CMake&#x811A;&#x672C;&#x9996;&#x5148;&#x8FDB;&#x884C;&#x914D;&#x7F6E;&#xFF0C;&#x7136;&#x540E;&#x8C03;&#x7528;Python&#x811A;&#x672C;&#x6253;&#x5370;<code>use_message</code>&#x53EF;&#x6267;&#x884C;&#x6587;&#x4EF6;&#x7684;<code>RPATH</code>:</p>
<pre><code class="lang-python"><span class="hljs-keyword">import</span> shlex
<span class="hljs-keyword">import</span> subprocess
<span class="hljs-keyword">import</span> sys

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">main</span><span class="hljs-params">()</span>:</span>
  patcher = sys.argv[<span class="hljs-number">1</span>]
  elfobj = sys.argv[<span class="hljs-number">2</span>]
  tools = {<span class="hljs-string">&apos;patchelf&apos;</span>: <span class="hljs-string">&apos;--print-rpath&apos;</span>, <span class="hljs-string">&apos;chrpath&apos;</span>: <span class="hljs-string">&apos;--list&apos;</span>, <span class="hljs-string">&apos;otool&apos;</span>: <span class="hljs-string">&apos;-L&apos;</span>}
  <span class="hljs-keyword">if</span> patcher <span class="hljs-keyword">not</span> <span class="hljs-keyword">in</span> tools.keys():
  <span class="hljs-keyword">raise</span> RuntimeError(<span class="hljs-string">&apos;Unknown tool {}&apos;</span>.format(patcher))
  cmd = shlex.split(<span class="hljs-string">&apos;{:s} {:s} {:s}&apos;</span>.format(patcher, tools[patcher], elfobj))
  rpath = subprocess.run(
      cmd,
      bufsize=<span class="hljs-number">1</span>,
      stdout=subprocess.PIPE,
      stderr=subprocess.PIPE,
      universal_newlines=<span class="hljs-keyword">True</span>)
  print(rpath.stdout)

<span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">&quot;__main__&quot;</span>:
  main()
</code></pre>
<p>&#x4F7F;&#x7528;&#x5E73;&#x53F0;&#x539F;&#x751F;&#x5DE5;&#x5177;&#x53EF;&#x4EE5;&#x8F7B;&#x677E;&#x5730;&#x6253;&#x5370;<code>RPATH</code>&#xFF0C;&#x7A0D;&#x540E;&#x6211;&#x4EEC;&#x5C06;&#x5728;&#x672C;&#x793A;&#x4F8B;&#x4E2D;&#x8BA8;&#x8BBA;&#x8FD9;&#x4E9B;&#x5DE5;&#x5177;&#x3002;</p>
<p>&#x6700;&#x540E;&#xFF0C;<code>src</code>&#x5B50;&#x76EE;&#x5F55;&#x5305;&#x542B;&#x9879;&#x76EE;&#x7684;<code>CMakeLists.txt</code>&#x548C;&#x6E90;&#x6587;&#x4EF6;&#x3002;<code>use_message.cpp</code>&#x6E90;&#x6587;&#x4EF6;&#x5305;&#x542B;&#x4EE5;&#x4E0B;&#x5185;&#x5BB9;:</p>
<pre><code class="lang-c++"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;cstdlib&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;iostream&gt;</span></span>

<span class="hljs-meta">#<span class="hljs-meta-keyword">ifdef</span> USING_message</span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;message/Message.hpp&gt;</span></span>
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">messaging</span><span class="hljs-params">()</span>
</span>{
  <span class="hljs-function">Message <span class="hljs-title">say_hello</span><span class="hljs-params">(<span class="hljs-string">&quot;Hello, World! From a client of yours!&quot;</span>)</span></span>;
  <span class="hljs-built_in">std</span>::<span class="hljs-built_in">cout</span> &lt;&lt; say_hello &lt;&lt; <span class="hljs-built_in">std</span>::<span class="hljs-built_in">endl</span>;
  <span class="hljs-function">Message <span class="hljs-title">say_goodbye</span><span class="hljs-params">(<span class="hljs-string">&quot;Goodbye, World! From a client of yours!&quot;</span>)</span></span>;
  <span class="hljs-built_in">std</span>::<span class="hljs-built_in">cout</span> &lt;&lt; say_goodbye &lt;&lt; <span class="hljs-built_in">std</span>::<span class="hljs-built_in">endl</span>;
}
<span class="hljs-meta">#<span class="hljs-meta-keyword">else</span></span>
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">messaging</span><span class="hljs-params">()</span>
</span>{
  <span class="hljs-built_in">std</span>::<span class="hljs-built_in">cout</span> &lt;&lt; <span class="hljs-string">&quot;Hello, World! From a client of yours!&quot;</span> &lt;&lt; <span class="hljs-built_in">std</span>::<span class="hljs-built_in">endl</span>;
  <span class="hljs-built_in">std</span>::<span class="hljs-built_in">cout</span> &lt;&lt; <span class="hljs-string">&quot;Goodbye, World! From a client of yours!&quot;</span> &lt;&lt; <span class="hljs-built_in">std</span>::<span class="hljs-built_in">endl</span>;
}
<span class="hljs-meta">#<span class="hljs-meta-keyword">endif</span></span>

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span>
</span>{
  messaging();
  <span class="hljs-keyword">return</span> EXIT_SUCCESS;
}
</code></pre>
<h2 id="&#x5177;&#x4F53;&#x5B9E;&#x65BD;">&#x5177;&#x4F53;&#x5B9E;&#x65BD;</h2>
<p>&#x6211;&#x4EEC;&#x5C06;&#x4ECE;&#x4E3B;<code>CMakeLists.txt</code>&#x6587;&#x4EF6;&#x5F00;&#x59CB;&#xFF0C;&#x5B83;&#x7528;&#x6765;&#x534F;&#x8C03;&#x8D85;&#x7EA7;&#x6784;&#x5EFA;:</p>
<ol>
<li><p>&#x4E0E;&#x4E4B;&#x524D;&#x7684;&#x793A;&#x4F8B;&#x76F8;&#x540C;&#x3002;&#x9996;&#x5148;&#x58F0;&#x660E;&#x4E00;&#x4E2A;C++11&#x9879;&#x76EE;&#xFF0C;&#x8BBE;&#x7F6E;&#x4E86;&#x9ED8;&#x8BA4;&#x5B89;&#x88C5;&#x8DEF;&#x5F84;&#x3001;&#x6784;&#x5EFA;&#x7C7B;&#x578B;&#x3001;&#x76EE;&#x6807;&#x7684;&#x8F93;&#x51FA;&#x76EE;&#x5F55;&#xFF0C;&#x4EE5;&#x53CA;&#x5B89;&#x88C5;&#x6811;&#x4E2D;&#x7EC4;&#x4EF6;&#x7684;&#x5E03;&#x5C40;:</p>
<pre><code class="lang-cmake"><span class="hljs-keyword">cmake_minimum_required</span>(VERSION <span class="hljs-number">3.6</span> FATAL_ERROR)

<span class="hljs-keyword">project</span>(recipe-<span class="hljs-number">04</span>
  LANGUAGES CXX
  VERSION <span class="hljs-number">1.0</span>.<span class="hljs-number">0</span>
  )

<span class="hljs-comment"># &lt;&lt;&lt; General set up &gt;&gt;&gt;</span>

<span class="hljs-keyword">set</span>(CMAKE_CXX_STANDARD <span class="hljs-number">11</span>)
<span class="hljs-keyword">set</span>(CMAKE_CXX_EXTENSIONS <span class="hljs-keyword">OFF</span>)
<span class="hljs-keyword">set</span>(CMAKE_CXX_STANDARD_REQUIRED <span class="hljs-keyword">ON</span>)

<span class="hljs-keyword">if</span>(NOT CMAKE_BUILD_TYPE)
  <span class="hljs-keyword">set</span>(CMAKE_BUILD_TYPE Release CACHE <span class="hljs-keyword">STRING</span> <span class="hljs-string">&quot;Build type&quot;</span> FORCE)
<span class="hljs-keyword">endif</span>()

<span class="hljs-keyword">message</span>(STATUS <span class="hljs-string">&quot;Build type set to ${CMAKE_BUILD_TYPE}&quot;</span>)

<span class="hljs-keyword">message</span>(STATUS <span class="hljs-string">&quot;Project will be installed to ${CMAKE_INSTALL_PREFIX}&quot;</span>)

<span class="hljs-keyword">include</span>(GNUInstallDirs)

<span class="hljs-keyword">set</span>(CMAKE_ARCHIVE_OUTPUT_DIRECTORY
  <span class="hljs-variable">${PROJECT_BINARY_DIR}</span>/<span class="hljs-variable">${CMAKE_INSTALL_LIBDIR}</span>)
<span class="hljs-keyword">set</span>(CMAKE_LIBRARY_OUTPUT_DIRECTORY
  <span class="hljs-variable">${PROJECT_BINARY_DIR}</span>/<span class="hljs-variable">${CMAKE_INSTALL_LIBDIR}</span>)
<span class="hljs-keyword">set</span>(CMAKE_RUNTIME_OUTPUT_DIRECTORY
  <span class="hljs-variable">${PROJECT_BINARY_DIR}</span>/<span class="hljs-variable">${CMAKE_INSTALL_BINDIR}</span>)

<span class="hljs-comment"># Offer the user the choice of overriding the installation directories</span>
<span class="hljs-keyword">set</span>(INSTALL_LIBDIR <span class="hljs-variable">${CMAKE_INSTALL_LIBDIR}</span> CACHE PATH <span class="hljs-string">&quot;Installation directory for libraries&quot;</span>)
<span class="hljs-keyword">set</span>(INSTALL_BINDIR <span class="hljs-variable">${CMAKE_INSTALL_BINDIR}</span> CACHE PATH <span class="hljs-string">&quot;Installation directory for executables&quot;</span>)
<span class="hljs-keyword">set</span>(INSTALL_INCLUDEDIR <span class="hljs-variable">${CMAKE_INSTALL_INCLUDEDIR}</span> CACHE PATH <span class="hljs-string">&quot;Installation directory for header files&quot;</span>)
<span class="hljs-keyword">if</span>(WIN32 <span class="hljs-keyword">AND</span> NOT CYGWIN)
  <span class="hljs-keyword">set</span>(DEF_INSTALL_CMAKEDIR CMake)
<span class="hljs-keyword">else</span>()
  <span class="hljs-keyword">set</span>(DEF_INSTALL_CMAKEDIR share/cmake/<span class="hljs-variable">${PROJECT_NAME}</span>)
<span class="hljs-keyword">endif</span>()
<span class="hljs-keyword">set</span>(INSTALL_CMAKEDIR <span class="hljs-variable">${DEF_INSTALL_CMAKEDIR}</span> CACHE PATH <span class="hljs-string">&quot;Installation directory for CMake files&quot;</span>)

<span class="hljs-comment"># Report to user</span>
<span class="hljs-keyword">foreach</span>(p LIB BIN <span class="hljs-keyword">INCLUDE</span> CMAKE)
  file(TO_NATIVE_PATH <span class="hljs-variable">${CMAKE_INSTALL_PREFIX}</span>/<span class="hljs-variable">${INSTALL_${p}</span>DIR} _path )
  <span class="hljs-keyword">message</span>(STATUS <span class="hljs-string">&quot;Installing ${p} components to ${_path}&quot;</span>)
  <span class="hljs-keyword">unset</span>(_path)
<span class="hljs-keyword">endforeach</span>()
</code></pre>
</li>
<li><p>&#x8BBE;&#x7F6E;&#x4E86;<code>EP_BASE</code>&#x76EE;&#x5F55;&#x5C5E;&#x6027;&#xFF0C;&#x8FD9;&#x5C06;&#x4E3A;&#x8D85;&#x6784;&#x5EFA;&#x4E2D;&#x7684;&#x5B50;&#x9879;&#x76EE;&#x8BBE;&#x7F6E;&#x5E03;&#x5C40;&#x3002;&#x6240;&#x6709;&#x5B50;&#x9879;&#x76EE;&#x90FD;&#x5C06;&#x5728;<code>CMAKE_BINARY_DIR</code>&#x7684;&#x5B50;&#x9879;&#x76EE;&#x6587;&#x4EF6;&#x5939;&#x4E0B;&#x751F;&#x6210;:</p>
<pre><code class="lang-cmake"><span class="hljs-keyword">set_property</span>(DIRECTORY PROPERTY EP_BASE <span class="hljs-variable">${CMAKE_BINARY_DIR}</span>/subprojects)
</code></pre>
</li>
<li><p>&#x7136;&#x540E;&#xFF0C;&#x58F0;&#x660E;<code>STAGED_INSTALL_PREFIX</code>&#x53D8;&#x91CF;&#x3002;&#x8FD9;&#x4E2A;&#x53D8;&#x91CF;&#x6307;&#x5411;&#x6784;&#x5EFA;&#x76EE;&#x5F55;&#x4E0B;&#x7684;<code>stage</code>&#x5B50;&#x76EE;&#x5F55;&#xFF0C;&#x9879;&#x76EE;&#x5C06;&#x5728;&#x6784;&#x5EFA;&#x671F;&#x95F4;&#x5B89;&#x88C5;&#x5728;&#x8FD9;&#x91CC;&#x3002;&#x8FD9;&#x662F;&#x4E00;&#x79CD;&#x6C99;&#x7BB1;&#x5B89;&#x88C5;&#x8FC7;&#x7A0B;&#xFF0C;&#x8BA9;&#x6211;&#x4EEC;&#x6709;&#x673A;&#x4F1A;&#x68C0;&#x67E5;&#x6574;&#x4E2A;&#x8D85;&#x7EA7;&#x6784;&#x5EFA;&#x7684;&#x5E03;&#x5C40;:</p>
<pre><code class="lang-cmake"><span class="hljs-keyword">set</span>(STAGED_INSTALL_PREFIX <span class="hljs-variable">${CMAKE_BINARY_DIR}</span>/stage)
<span class="hljs-keyword">message</span>(STATUS <span class="hljs-string">&quot;${PROJECT_NAME} staged install: ${STAGED_INSTALL_PREFIX}&quot;</span>)
</code></pre>
</li>
<li><p>&#x6DFB;&#x52A0;<code>external/upstream</code>&#x5B50;&#x76EE;&#x5F55;&#x3002;&#x5176;&#x4E2D;&#x5305;&#x62EC;&#x4F7F;&#x7528;CMake&#x6307;&#x4EE4;&#x6765;&#x7BA1;&#x7406;&#x6211;&#x4EEC;&#x7684;&#x4E0A;&#x6E38;&#x4F9D;&#x8D56;&#x5173;&#x7CFB;&#xFF0C;&#x5728;&#x6211;&#x4EEC;&#x7684;&#x4F8B;&#x5B50;&#x4E2D;&#xFF0C;&#x5C31;&#x662F;&#x6D88;&#x606F;&#x5E93;:</p>
<pre><code class="lang-cmake"><span class="hljs-keyword">add_subdirectory</span>(external/upstream)
</code></pre>
</li>
<li><p>&#x7136;&#x540E;&#xFF0C;&#x5305;&#x542B;<code>ExternalProject.cmake</code>&#x6807;&#x51C6;&#x6A21;&#x5757;:</p>
<pre><code class="lang-cmake"><span class="hljs-keyword">include</span>(ExternalProject)
</code></pre>
</li>
<li><p>&#x5C06;&#x81EA;&#x5DF1;&#x7684;&#x9879;&#x76EE;&#x4F5C;&#x4E3A;&#x5916;&#x90E8;&#x9879;&#x76EE;&#x6DFB;&#x52A0;&#xFF0C;&#x8C03;&#x7528;<code>ExternalProject_Add</code>&#x547D;&#x4EE4;&#x3002;<code>SOURCE_DIR</code>&#x7528;&#x4E8E;&#x6307;&#x5B9A;&#x6E90;&#x4F4D;&#x4E8E;<code>src</code>&#x5B50;&#x76EE;&#x5F55;&#x4E2D;&#x3002;&#x6211;&#x4EEC;&#x4F1A;&#x9009;&#x62E9;&#x9002;&#x5F53;&#x7684;CMake&#x53C2;&#x6570;&#x6765;&#x914D;&#x7F6E;&#x6211;&#x4EEC;&#x7684;&#x9879;&#x76EE;&#x3002;&#x8FD9;&#x91CC;&#xFF0C;&#x4F7F;&#x7528;<code>STAGED_INSTALL_PREFIX</code>&#x4F5C;&#x4E3A;&#x5B50;&#x9879;&#x76EE;&#x7684;&#x5B89;&#x88C5;&#x76EE;&#x5F55;:</p>
<pre><code class="lang-cmake">ExternalProject_Add(<span class="hljs-variable">${PROJECT_NAME}</span>_core
  DEPENDS
    message_external
  SOURCE_DIR
    <span class="hljs-variable">${CMAKE_CURRENT_SOURCE_DIR}</span>/src
  CMAKE_ARGS
    -DCMAKE_INSTALL_PREFIX=<span class="hljs-variable">${STAGED_INSTALL_PREFIX}</span>
    -DCMAKE_BUILD_TYPE=<span class="hljs-variable">${CMAKE_BUILD_TYPE}</span>
    -DCMAKE_CXX_COMPILER=<span class="hljs-variable">${CMAKE_CXX_COMPILER}</span>
    -DCMAKE_CXX_FLAGS=<span class="hljs-variable">${CMAKE_CXX_FLAGS}</span>
    -DCMAKE_CXX_STANDARD=<span class="hljs-variable">${CMAKE_CXX_STANDARD}</span>
    -DCMAKE_CXX_EXTENSIONS=<span class="hljs-variable">${CMAKE_CXX_EXTENSIONS}</span>
    -DCMAKE_CXX_STANDARD_REQUIRED=<span class="hljs-variable">${CMAKE_CXX_STANDARD_REQUIRED}</span>
    -Dmessage_DIR=<span class="hljs-variable">${message_DIR}</span>
  CMAKE_CACHE_ARGS
    -DCMAKE_PREFIX_PATH:PATH=<span class="hljs-variable">${CMAKE_PREFIX_PATH}</span>
  BUILD_ALWAYS
    <span class="hljs-number">1</span>
  )
</code></pre>
</li>
<li><p>&#x73B0;&#x5728;&#xFF0C;&#x4E3A;<code>use_message</code>&#x6DFB;&#x52A0;&#x4E00;&#x4E2A;&#x6D4B;&#x8BD5;&#xFF0C;&#x5E76;&#x7531;<code>recipe-04_core</code>&#x6784;&#x5EFA;&#x3002;&#x8FD9;&#x5C06;&#x8FD0;&#x884C;<code>use_message</code>&#x53EF;&#x6267;&#x884C;&#x6587;&#x4EF6;&#x7684;&#x5B89;&#x88C5;&#xFF0C;&#x5373;&#x4F4D;&#x4E8E;&#x6784;&#x5EFA;&#x6811;&#x4E2D;&#x7684;&#x5B89;&#x88C5;:</p>
<pre><code class="lang-cmake"><span class="hljs-keyword">enable_testing</span>()

<span class="hljs-keyword">add_test</span>(
  NAME
      check_use_message
  COMMAND
      <span class="hljs-variable">${STAGED_INSTALL_PREFIX}</span>/<span class="hljs-variable">${INSTALL_BINDIR}</span>/use_message
  )
</code></pre>
</li>
<li><p>&#x6700;&#x540E;&#xFF0C;&#x53EF;&#x4EE5;&#x58F0;&#x660E;&#x5B89;&#x88C5;&#x89C4;&#x5219;&#x3002;&#x56E0;&#x4E3A;&#x6240;&#x9700;&#x8981;&#x7684;&#x4E1C;&#x897F;&#x90FD;&#x5DF2;&#x7ECF;&#x5B89;&#x88C5;&#x5728;&#x6682;&#x5B58;&#x533A;&#x57DF;&#x4E2D;&#xFF0C;&#x6211;&#x4EEC;&#x53EA;&#x8981;&#x5C06;&#x6682;&#x5B58;&#x533A;&#x57DF;&#x7684;&#x5185;&#x5BB9;&#x590D;&#x5236;&#x5230;&#x5B89;&#x88C5;&#x76EE;&#x5F55;&#x5373;&#x53EF;:</p>
<pre><code class="lang-cmake"><span class="hljs-keyword">install</span>(
  DIRECTORY
      <span class="hljs-variable">${STAGED_INSTALL_PREFIX}</span>/
  DESTINATION
      .
  USE_SOURCE_PERMISSIONS
  )
</code></pre>
</li>
<li><p>&#x4F7F;&#x7528;<code>SCRIPT</code>&#x53C2;&#x6570;&#x58F0;&#x660E;&#x4E00;&#x4E2A;&#x9644;&#x52A0;&#x7684;&#x5B89;&#x88C5;&#x89C4;&#x5219;&#x3002;CMake&#x811A;&#x672C;&#x7684;<code>install_hook.cmake</code>&#x5C06;&#x88AB;&#x6267;&#x884C;&#xFF0C;&#x4F46;&#x53EA;&#x5728;GNU/Linux&#x548C;macOS&#x4E0A;&#x6267;&#x884C;&#x3002;&#x8FD9;&#x4E2A;&#x811A;&#x672C;&#x5C06;&#x6253;&#x5370;&#x5DF2;&#x5B89;&#x88C5;&#x7684;&#x53EF;&#x6267;&#x884C;&#x6587;&#x4EF6;&#x7684;<code>RPATH</code>&#xFF0C;&#x5E76;&#x8FD0;&#x884C;&#x5B83;&#x3002;&#x6211;&#x4EEC;&#x5C06;&#x5728;&#x4E0B;&#x4E00;&#x8282;&#x8BE6;&#x7EC6;&#x5730;&#x8BA8;&#x8BBA;&#x8FD9;&#x4E2A;&#x95EE;&#x9898;&#xFF1A;</p>
<pre><code class="lang-cmake"><span class="hljs-keyword">if</span>(UNIX)
  <span class="hljs-keyword">set</span>(PRINT_SCRIPT <span class="hljs-string">&quot;${CMAKE_CURRENT_LIST_DIR}/cmake/print_rpath.py&quot;</span>)
  <span class="hljs-keyword">configure_file</span>(cmake/install_hook.cmake.in install_hook.cmake @ONLY)
  <span class="hljs-keyword">install</span>(
    SCRIPT
      <span class="hljs-variable">${CMAKE_CURRENT_BINARY_DIR}</span>/install_hook.cmake
    )
<span class="hljs-keyword">endif</span>()
</code></pre>
</li>
</ol>
<p><code>-Dmessage_DIR=${message_DIR}</code>&#x5DF2;&#x4F5C;&#x4E3A;CMake&#x53C2;&#x6570;&#x4F20;&#x9012;&#x7ED9;&#x9879;&#x76EE;&#xFF0C;&#x8FD9;&#x5C06;&#x6B63;&#x786E;&#x8BBE;&#x7F6E;&#x6D88;&#x606F;&#x5E93;&#x4F9D;&#x8D56;&#x9879;&#x7684;&#x4F4D;&#x7F6E;&#x3002;<code>message_DIR</code>&#x7684;&#x503C;&#x5728;<code>external/upstream/message</code>&#x76EE;&#x5F55;&#x4E0B;&#x7684;<code>CMakeLists.txt</code>&#x6587;&#x4EF6;&#x4E2D;&#x5B9A;&#x4E49;&#x3002;&#x8FD9;&#x4E2A;&#x6587;&#x4EF6;&#x5904;&#x7406;&#x4F9D;&#x8D56;&#x4E8E;&#x6D88;&#x606F;&#x5E93;&#xFF0C;&#x8BA9;&#x6211;&#x4EEC;&#x770B;&#x770B;&#x662F;&#x5982;&#x4F55;&#x5904;&#x7406;&#x7684;:</p>
<ol>
<li><p>&#x9996;&#x5148;&#xFF0C;&#x641C;&#x7D22;&#x5E76;&#x627E;&#x5230;&#x5305;&#x3002;&#x7528;&#x6237;&#x53EF;&#x80FD;&#x5DF2;&#x7ECF;&#x5728;&#x7CFB;&#x7EDF;&#x7684;&#x67D0;&#x4E2A;&#x5730;&#x65B9;&#x5B89;&#x88C5;&#x4E86;&#xFF0C;&#x5E76;&#x5728;&#x914D;&#x7F6E;&#x65F6;&#x4F20;&#x9012;&#x4E86;<code>message_DIR</code>:</p>
<pre><code class="lang-cmake"><span class="hljs-keyword">find_package</span>(<span class="hljs-keyword">message</span> <span class="hljs-number">1</span> CONFIG QUIET)
</code></pre>
</li>
<li><p>&#x5982;&#x679C;&#x627E;&#x5230;&#x4E86;&#x6D88;&#x606F;&#x5E93;&#xFF0C;&#x6211;&#x4EEC;&#x5C06;&#x5411;&#x7528;&#x6237;&#x62A5;&#x544A;&#x76EE;&#x6807;&#x7684;&#x4F4D;&#x7F6E;&#x548C;&#x7248;&#x672C;&#xFF0C;&#x5E76;&#x6DFB;&#x52A0;&#x4E00;&#x4E2A;&#x865A;&#x62DF;&#x7684;<code>message_external</code>&#x76EE;&#x6807;&#x3002;&#x8FD9;&#x91CC;&#xFF0C;&#x9700;&#x8981;&#x865A;&#x62DF;&#x76EE;&#x6807;&#x6765;&#x6B63;&#x786E;&#x5904;&#x7406;&#x8D85;&#x6784;&#x5EFA;&#x7684;&#x4F9D;&#x8D56;&#x5173;&#x7CFB;:</p>
<pre><code class="lang-cmake"><span class="hljs-keyword">if</span>(message_FOUND)
  <span class="hljs-keyword">get_property</span>(_loc TARGET <span class="hljs-keyword">message</span>::<span class="hljs-keyword">message</span>-shared PROPERTY LOCATION)
  <span class="hljs-keyword">message</span>(STATUS <span class="hljs-string">&quot;Found message: ${_loc} (found version ${message_VERSION})&quot;</span>)
  <span class="hljs-keyword">add_library</span>(message_external INTERFACE) <span class="hljs-comment"># dummy</span>
</code></pre>
</li>
<li><p>&#x5982;&#x679C;&#x6CA1;&#x6709;&#x627E;&#x5230;&#x8FD9;&#x4E2A;&#x5E93;&#xFF0C;&#x6211;&#x4EEC;&#x5C06;&#x628A;&#x5B83;&#x6DFB;&#x52A0;&#x4E3A;&#x4E00;&#x4E2A;&#x5916;&#x90E8;&#x9879;&#x76EE;&#xFF0C;&#x4ECE;&#x5728;&#x7EBF;Git&#x5B58;&#x50A8;&#x5E93;&#x4E0B;&#x8F7D;&#x5B83;&#xFF0C;&#x7136;&#x540E;&#x7F16;&#x8BD1;&#x5B83;&#x3002;&#x5B89;&#x88C5;&#x8DEF;&#x5F84;&#x3001;&#x6784;&#x5EFA;&#x7C7B;&#x578B;&#x548C;&#x5B89;&#x88C5;&#x76EE;&#x5F55;&#x5E03;&#x5C40;&#x90FD;&#x662F;&#x7531;&#x4E3B;<code>CMakeLists.txt</code>&#x6587;&#x4EF6;&#x8BBE;&#x7F6E;&#xFF0C;C++&#x7F16;&#x8BD1;&#x5668;&#x548C;&#x6807;&#x5FD7;&#x4E5F;&#x662F;&#x5982;&#x6B64;&#x3002;&#x9879;&#x76EE;&#x5C06;&#x5B89;&#x88C5;&#x5230;<code>STAGED_INSTALL_PREFIX</code>&#x4E0B;&#xFF0C;&#x7136;&#x540E;&#x8FDB;&#x884C;&#x6D4B;&#x8BD5;:</p>
<pre><code class="lang-cmake"><span class="hljs-keyword">else</span>()
  <span class="hljs-keyword">include</span>(ExternalProject)
  <span class="hljs-keyword">message</span>(STATUS <span class="hljs-string">&quot;Suitable message could not be located, Building message instead.&quot;</span>)
  ExternalProject_Add(message_external
    GIT_REPOSITORY
      https://github.com/dev-cafe/<span class="hljs-keyword">message</span>.git
    GIT_TAG
      master
    UPDATE_COMMAND
      <span class="hljs-string">&quot;&quot;</span>
    CMAKE_ARGS
      -DCMAKE_INSTALL_PREFIX=<span class="hljs-variable">${STAGED_INSTALL_PREFIX}</span>
      -DCMAKE_BUILD_TYPE=<span class="hljs-variable">${CMAKE_BUILD_TYPE}</span>
      -DCMAKE_CXX_COMPILER=<span class="hljs-variable">${CMAKE_CXX_COMPILER}</span>
    CMAKE_CACHE_ARGS
      -DCMAKE_CXX_FLAGS:<span class="hljs-keyword">STRING</span>=<span class="hljs-variable">${CMAKE_CXX_FLAGS}</span>
    TEST_AFTER_INSTALL
      <span class="hljs-number">1</span>
    DOWNLOAD_NO_PROGRESS
      <span class="hljs-number">1</span>
    LOG_CONFIGURE
      <span class="hljs-number">1</span>
    LOG_BUILD
      <span class="hljs-number">1</span>
    LOG_INSTALL
      <span class="hljs-number">1</span>
  )
</code></pre>
</li>
<li><p>&#x6700;&#x540E;&#xFF0C;&#x5C06;<code>message_DIR</code>&#x76EE;&#x5F55;&#x8FDB;&#x884C;&#x8BBE;&#x7F6E;&#xFF0C;&#x4E3A;&#x6307;&#x5411;&#x65B0;&#x6784;&#x5EFA;&#x7684;<code>messageConfig.cmake</code>&#x6587;&#x4EF6;&#x6307;&#x660E;&#x5B89;&#x88C5;&#x8DEF;&#x5F84;&#x3002;&#x6CE8;&#x610F;&#xFF0C;&#x8FD9;&#x4E9B;&#x8DEF;&#x5F84;&#x88AB;&#x4FDD;&#x5B58;&#x5230;<code>CMakeCache</code>&#x4E2D;:</p>
<pre><code class="lang-cmake">  <span class="hljs-keyword">if</span>(WIN32 <span class="hljs-keyword">AND</span> NOT CYGWIN)
    <span class="hljs-keyword">set</span>(DEF_message_DIR <span class="hljs-variable">${STAGED_INSTALL_PREFIX}</span>/CMake)
  <span class="hljs-keyword">else</span>()
    <span class="hljs-keyword">set</span>(DEF_message_DIR <span class="hljs-variable">${STAGED_INSTALL_PREFIX}</span>/share/cmake/<span class="hljs-keyword">message</span>)
  <span class="hljs-keyword">endif</span>()
  file(TO_NATIVE_PATH <span class="hljs-string">&quot;${DEF_message_DIR}&quot;</span> DEF_message_DIR)
  <span class="hljs-keyword">set</span>(message_DIR <span class="hljs-variable">${DEF_message_DIR}</span>
    CACHE PATH <span class="hljs-string">&quot;Path to internally built messageConfig.cmake&quot;</span> FORCE)
<span class="hljs-keyword">endif</span>()
</code></pre>
</li>
</ol>
<p>&#x6211;&#x4EEC;&#x7EC8;&#x4E8E;&#x51C6;&#x5907;&#x597D;&#x7F16;&#x8BD1;&#x6211;&#x4EEC;&#x81EA;&#x5DF1;&#x7684;&#x9879;&#x76EE;&#xFF0C;&#x5E76;&#x6210;&#x529F;&#x5730;&#x5C06;&#x5176;&#x94FE;&#x63A5;&#x5230;&#x6D88;&#x606F;&#x5E93;(&#x65E0;&#x8BBA;&#x662F;&#x7CFB;&#x7EDF;&#x4E0A;&#x5DF2;&#x6709;&#x7684;&#x6D88;&#x606F;&#x5E93;&#xFF0C;&#x8FD8;&#x662F;&#x65B0;&#x6784;&#x5EFA;&#x7684;&#x6D88;&#x606F;&#x5E93;)&#x3002;&#x7531;&#x4E8E;&#x8FD9;&#x662F;&#x4E00;&#x4E2A;&#x8D85;&#x7EA7;&#x6784;&#x5EFA;&#xFF0C;<code>src</code>&#x5B50;&#x76EE;&#x5F55;&#x4E0B;&#x7684;&#x4EE3;&#x7801;&#x662F;&#x4E00;&#x4E2A;&#x5B8C;&#x5168;&#x72EC;&#x7ACB;&#x7684;CMake&#x9879;&#x76EE;:</p>
<ol>
<li><p>&#x58F0;&#x660E;&#x4E00;&#x4E2A;C++11&#x9879;&#x76EE;&#xFF1A;</p>
<pre><code class="lang-cmake"><span class="hljs-keyword">cmake_minimum_required</span>(VERSION <span class="hljs-number">3.6</span> FATAL_ERROR)

<span class="hljs-keyword">project</span>(recipe-<span class="hljs-number">04</span>_core
  LANGUAGES CXX
  )

<span class="hljs-keyword">set</span>(CMAKE_CXX_STANDARD <span class="hljs-number">11</span>)
<span class="hljs-keyword">set</span>(CMAKE_CXX_EXTENSIONS <span class="hljs-keyword">OFF</span>)
<span class="hljs-keyword">set</span>(CMAKE_CXX_STANDARD_REQUIRED <span class="hljs-keyword">ON</span>)

<span class="hljs-keyword">include</span>(GNUInstallDirs)

<span class="hljs-keyword">set</span>(CMAKE_ARCHIVE_OUTPUT_DIRECTORY
  <span class="hljs-variable">${CMAKE_BINARY_DIR}</span>/<span class="hljs-variable">${CMAKE_INSTALL_LIBDIR}</span>)
<span class="hljs-keyword">set</span>(CMAKE_LIBRARY_OUTPUT_DIRECTORY
  <span class="hljs-variable">${CMAKE_BINARY_DIR}</span>/<span class="hljs-variable">${CMAKE_INSTALL_LIBDIR}</span>)
<span class="hljs-keyword">set</span>(CMAKE_RUNTIME_OUTPUT_DIRECTORY
  <span class="hljs-variable">${CMAKE_BINARY_DIR}</span>/<span class="hljs-variable">${CMAKE_INSTALL_BINDIR}</span>)
</code></pre>
</li>
<li><p>&#x5C1D;&#x8BD5;&#x627E;&#x5230;&#x6D88;&#x606F;&#x5E93;&#x3002;&#x8D85;&#x7EA7;&#x6784;&#x5EFA;&#x4E2D;&#xFF0C;&#x6B63;&#x786E;&#x8BBE;&#x7F6E;<code>message_DIR</code>:</p>
<pre><code class="lang-cmake"><span class="hljs-keyword">find_package</span>(<span class="hljs-keyword">message</span> <span class="hljs-number">1</span> CONFIG REQUIRED)
<span class="hljs-keyword">get_property</span>(_loc TARGET <span class="hljs-keyword">message</span>::<span class="hljs-keyword">message</span>-shared PROPERTY LOCATION)
<span class="hljs-keyword">message</span>(STATUS <span class="hljs-string">&quot;Found message: ${_loc} (found version ${message_VERSION})&quot;</span>)
</code></pre>
</li>
<li><p>&#x6DFB;&#x52A0;&#x53EF;&#x6267;&#x884C;&#x76EE;&#x6807;<code>use_message</code>&#xFF0C;&#x8BE5;&#x76EE;&#x6807;&#x7531;<code>use_message.cpp</code>&#x6E90;&#x6587;&#x4EF6;&#x521B;&#x5EFA;&#xFF0C;&#x5E76;&#x8FDE;&#x63A5;&#x5230;<code>message::message-shared</code>&#x76EE;&#x6807;:</p>
<pre><code class="lang-cmake"><span class="hljs-keyword">add_executable</span>(use_message use_message.cpp)

<span class="hljs-keyword">target_link_libraries</span>(use_message
  PUBLIC
      <span class="hljs-keyword">message</span>::<span class="hljs-keyword">message</span>-shared
  )
</code></pre>
</li>
<li><p>&#x4E3A;<code>use_message</code>&#x8BBE;&#x7F6E;&#x76EE;&#x6807;&#x5C5E;&#x6027;&#x3002;&#x518D;&#x6B21;&#x5BF9;<code>RPATH</code>&#x8FDB;&#x884C;&#x8BBE;&#x7F6E;:</p>
<pre><code class="lang-cmake"><span class="hljs-comment"># Prepare RPATH</span>
file(RELATIVE_PATH _rel <span class="hljs-variable">${CMAKE_INSTALL_PREFIX}</span>/<span class="hljs-variable">${CMAKE_INSTALL_BINDIR}</span> <span class="hljs-variable">${CMAKE_INSTALL_PREFIX}</span>)
<span class="hljs-keyword">if</span>(APPLE)
  <span class="hljs-keyword">set</span>(_rpath <span class="hljs-string">&quot;@loader_path/${_rel}&quot;</span>)
<span class="hljs-keyword">else</span>()
  <span class="hljs-keyword">set</span>(_rpath <span class="hljs-string">&quot;\$ORIGIN/${_rel}&quot;</span>)
<span class="hljs-keyword">endif</span>()
file(TO_NATIVE_PATH <span class="hljs-string">&quot;${_rpath}/${CMAKE_INSTALL_LIBDIR}&quot;</span> use_message_RPATH)

<span class="hljs-keyword">set_target_properties</span>(use_message
  PROPERTIES
    MACOSX_RPATH <span class="hljs-keyword">ON</span>
    SKIP_BUILD_RPATH <span class="hljs-keyword">OFF</span>
    BUILD_WITH_INSTALL_RPATH <span class="hljs-keyword">OFF</span>
    INSTALL_RPATH <span class="hljs-string">&quot;${use_message_RPATH}&quot;</span>
    INSTALL_RPATH_USE_LINK_PATH <span class="hljs-keyword">ON</span>
  )
</code></pre>
</li>
<li><p>&#x6700;&#x540E;&#xFF0C;&#x4E3A;<code>use_message</code>&#x76EE;&#x6807;&#x8BBE;&#x7F6E;&#x4E86;&#x5B89;&#x88C5;&#x89C4;&#x5219;:</p>
<pre><code class="lang-cmake"><span class="hljs-keyword">install</span>(
  TARGETS
      use_message
  RUNTIME
    DESTINATION <span class="hljs-variable">${CMAKE_INSTALL_BINDIR}</span>
    COMPONENT bin
  )
</code></pre>
</li>
</ol>
<p>&#x73B0;&#x5728;&#x77A7;&#x77A7;CMake&#x811A;&#x672C;&#x6A21;&#x677F;<code>install_hook.cmake.in</code>&#x7684;&#x5185;&#x5BB9;&#xFF1A;</p>
<ol>
<li><p>CMake&#x811A;&#x672C;&#x5728;&#x6211;&#x4EEC;&#x7684;&#x4E3B;&#x9879;&#x76EE;&#x8303;&#x56F4;&#x4E4B;&#x5916;&#x6267;&#x884C;&#xFF0C;&#x56E0;&#x6B64;&#x6CA1;&#x6709;&#x5B9A;&#x4E49;&#x53D8;&#x91CF;&#x6216;&#x76EE;&#x6807;&#x7684;&#x6982;&#x5FF5;&#x3002;&#x56E0;&#x6B64;&#xFF0C;&#x9700;&#x8981;&#x8BBE;&#x7F6E;&#x53D8;&#x91CF;&#x6765;&#x4FDD;&#x5B58;&#x5DF2;&#x5B89;&#x88C5;&#x7684;<code>use_message</code>&#x53EF;&#x6267;&#x884C;&#x6587;&#x4EF6;&#x7684;&#x5B8C;&#x6574;&#x8DEF;&#x5F84;&#x3002;&#x6CE8;&#x610F;&#x4F7F;&#x7528;<code>@INSTALL_BINDIR@</code>&#xFF0C;&#x5B83;&#x5C06;&#x7531;<code>configure_file</code>&#x89E3;&#x6790;&#xFF1A;</p>
<pre><code class="lang-cmake"><span class="hljs-keyword">set</span>(_executable <span class="hljs-variable">${CMAKE_INSTALL_PREFIX}</span>/@INSTALL_BINDIR@/use_message)
</code></pre>
</li>
<li><p>&#x9700;&#x8981;&#x627E;&#x5230;&#x5E73;&#x53F0;&#x672C;&#x673A;&#x53EF;&#x6267;&#x884C;&#x5DE5;&#x5177;&#xFF0C;&#x4F7F;&#x7528;&#x8BE5;&#x5DE5;&#x5177;&#x6253;&#x5370;&#x5DF2;&#x5B89;&#x88C5;&#x7684;&#x53EF;&#x6267;&#x884C;&#x6587;&#x4EF6;&#x7684;<code>RPATH</code>&#x3002;&#x6211;&#x4EEC;&#x5C06;&#x641C;&#x7D22;<code>chrpath</code>&#x3001;<code>patchelf</code>&#x548C;<code>otool</code>&#x3002;&#x5F53;&#x627E;&#x5230;&#x5DF2;&#x5B89;&#x88C5;&#x7684;&#x7A0B;&#x5E8F;&#x65F6;&#xFF0C;&#x5411;&#x7528;&#x6237;&#x63D0;&#x4F9B;&#x6709;&#x7528;&#x7684;&#x72B6;&#x6001;&#x4FE1;&#x606F;&#xFF0C;&#x5E76;&#x4E14;&#x9000;&#x51FA;&#x641C;&#x7D22;&#xFF1A;</p>
<pre><code class="lang-cmake"><span class="hljs-keyword">set</span>(_patcher)
list(APPEND _patchers chrpath patchelf otool)
<span class="hljs-keyword">foreach</span>(p IN LISTS _patchers)
  <span class="hljs-keyword">find_program</span>(<span class="hljs-variable">${p}</span>_FOUND
    NAMES
      <span class="hljs-variable">${p}</span>
    )
  <span class="hljs-keyword">if</span>(<span class="hljs-variable">${p}</span>_FOUND)
    <span class="hljs-keyword">set</span>(_patcher <span class="hljs-variable">${p}</span>)
    <span class="hljs-keyword">message</span>(STATUS <span class="hljs-string">&quot;ELF patching tool ${_patcher} FOUND&quot;</span>)
    <span class="hljs-keyword">break</span>()
  <span class="hljs-keyword">endif</span>()
<span class="hljs-keyword">endforeach</span>()
</code></pre>
</li>
<li><p>&#x68C0;&#x67E5;<code>_patcher</code>&#x53D8;&#x91CF;&#x662F;&#x5426;&#x4E3A;&#x7A7A;&#xFF0C;&#x8FD9;&#x610F;&#x5473;&#x7740;PatchELF&#x5DE5;&#x5177;&#x662F;&#x5426;&#x53EF;&#x7528;&#x3002;&#x5F53;&#x4E3A;&#x7A7A;&#x65F6;&#xFF0C;&#x6211;&#x4EEC;&#x8981;&#x8FDB;&#x884C;&#x7684;&#x64CD;&#x4F5C;&#x5C06;&#x4F1A;&#x5931;&#x8D25;&#xFF0C;&#x6240;&#x4EE5;&#x4F1A;&#x53D1;&#x51FA;&#x4E00;&#x4E2A;&#x81F4;&#x547D;&#x9519;&#x8BEF;&#xFF0C;&#x63D0;&#x9192;&#x7528;&#x6237;&#x9700;&#x8981;&#x5B89;&#x88C5;PatchELF&#x5DE5;&#x5177;:</p>
<pre><code class="lang-cmake"><span class="hljs-keyword">if</span>(NOT _patcher)
    <span class="hljs-keyword">message</span>(FATAL_ERROR <span class="hljs-string">&quot;ELF patching tool NOT FOUND!\nPlease install one of chrpath, patchelf or otool&quot;</span>)
</code></pre>
</li>
<li><p>&#x5F53;PatchELF&#x5DE5;&#x5177;&#x627E;&#x5230;&#x4E86;&#xFF0C;&#x5219;&#x7EE7;&#x7EED;&#x3002;&#x6211;&#x4EEC;&#x8C03;&#x7528;Python&#x811A;&#x672C;<code>print_rpath.py</code>&#xFF0C;&#x5C06;<code>_executable</code>&#x53D8;&#x91CF;&#x4F5C;&#x4E3A;&#x53C2;&#x6570;&#x4F20;&#x9012;&#x7ED9;<code>execute_process</code>&#xFF1A;</p>
<pre><code class="lang-cmake">  <span class="hljs-keyword">find_package</span>(PythonInterp REQUIRED QUIET)
  <span class="hljs-keyword">execute_process</span>(
    COMMAND
      <span class="hljs-variable">${PYTHON_EXECUTABLE}</span> @PRINT_SCRIPT@ <span class="hljs-string">&quot;${_patcher}&quot;</span>
    <span class="hljs-string">&quot;${_executable}&quot;</span>
    RESULT_VARIABLE _res
    OUTPUT_VARIABLE _out
    ERROR_VARIABLE _err
    OUTPUT_STRIP_TRAILING_WHITESPACE
    )
</code></pre>
</li>
<li><p>&#x68C0;&#x67E5;<code>_res</code>&#x53D8;&#x91CF;&#x7684;&#x8FD4;&#x56DE;&#x4EE3;&#x7801;&#x3002;&#x5982;&#x679C;&#x6267;&#x884C;&#x6210;&#x529F;&#xFF0C;&#x5C06;&#x6253;&#x5370;<code>_out</code>&#x53D8;&#x91CF;&#x4E2D;&#x6355;&#x83B7;&#x7684;&#x6807;&#x51C6;&#x8F93;&#x51FA;&#x6D41;&#x3002;&#x5426;&#x5219;&#xFF0C;&#x6253;&#x5370;&#x9000;&#x51FA;&#x524D;&#x6355;&#x83B7;&#x7684;&#x6807;&#x51C6;&#x8F93;&#x51FA;&#x548C;&#x9519;&#x8BEF;&#x6D41;:</p>
<pre><code class="lang-cmake">  <span class="hljs-keyword">if</span>(_res <span class="hljs-keyword">EQUAL</span> <span class="hljs-number">0</span>)
    <span class="hljs-keyword">message</span>(STATUS <span class="hljs-string">&quot;RPATH for ${_executable} is ${_out}&quot;</span>)
  <span class="hljs-keyword">else</span>()
    <span class="hljs-keyword">message</span>(STATUS <span class="hljs-string">&quot;Something went wrong!&quot;</span>)
    <span class="hljs-keyword">message</span>(STATUS <span class="hljs-string">&quot;Standard output from print_rpath.py: ${_out}&quot;</span>)
    <span class="hljs-keyword">message</span>(STATUS <span class="hljs-string">&quot;Standard error from print_rpath.py: ${_err}&quot;</span>)
    <span class="hljs-keyword">message</span>(FATAL_ERROR <span class="hljs-string">&quot;${_patcher} could NOT obtain RPATH for ${_executable}&quot;</span>)
  <span class="hljs-keyword">endif</span>()
<span class="hljs-keyword">endif</span>()
</code></pre>
</li>
<li><p>&#x518D;&#x4F7F;&#x7528;<code>execute_process</code>&#x6765;&#x8FD0;&#x884C;&#x5DF2;&#x5B89;&#x88C5;&#x7684;<code>use_message</code>&#x53EF;&#x6267;&#x884C;&#x76EE;&#x6807;:</p>
<pre><code class="lang-cmake"><span class="hljs-keyword">execute_process</span>(
  COMMAND <span class="hljs-variable">${_executable}</span>
  RESULT_VARIABLE _res
  OUTPUT_VARIABLE _out
  ERROR_VARIABLE _err
  OUTPUT_STRIP_TRAILING_WHITESPACE
  )
</code></pre>
</li>
<li><p>&#x6700;&#x540E;&#xFF0C;&#x5411;&#x7528;&#x6237;&#x62A5;&#x544A;<code>execute_process</code>&#x7684;&#x7ED3;&#x679C;:</p>
<pre><code class="lang-cmake"><span class="hljs-keyword">if</span>(_res <span class="hljs-keyword">EQUAL</span> <span class="hljs-number">0</span>)
  <span class="hljs-keyword">message</span>(STATUS <span class="hljs-string">&quot;Running ${_executable}:\n ${_out}&quot;</span>)
<span class="hljs-keyword">else</span>()
  <span class="hljs-keyword">message</span>(STATUS <span class="hljs-string">&quot;Something went wrong!&quot;</span>)
  <span class="hljs-keyword">message</span>(STATUS <span class="hljs-string">&quot;Standard output from running ${_executable}:\n ${_out}&quot;</span>)
  <span class="hljs-keyword">message</span>(STATUS <span class="hljs-string">&quot;Standard error from running ${_executable}:\n ${_err}&quot;</span>)
  <span class="hljs-keyword">message</span>(FATAL_ERROR <span class="hljs-string">&quot;Something went wrong with ${_executable}&quot;</span>)
<span class="hljs-keyword">endif</span>()
</code></pre>
</li>
</ol>
<h2 id="&#x5DE5;&#x4F5C;&#x539F;&#x7406;">&#x5DE5;&#x4F5C;&#x539F;&#x7406;</h2>
<p>CMake&#x5DE5;&#x5177;&#x7BB1;&#x4E2D;&#xFF0C;&#x8D85;&#x7EA7;&#x6784;&#x5EFA;&#x662F;&#x975E;&#x5E38;&#x6709;&#x7528;&#x7684;&#x6A21;&#x5F0F;&#x3002;&#x5B83;&#x901A;&#x8FC7;&#x5C06;&#x590D;&#x6742;&#x7684;&#x9879;&#x76EE;&#x5212;&#x5206;&#x4E3A;&#x66F4;&#x5C0F;&#x3001;&#x66F4;&#x5BB9;&#x6613;&#x7BA1;&#x7406;&#x7684;&#x5B50;&#x9879;&#x76EE;&#x6765;&#x7BA1;&#x7406;&#x5B83;&#x4EEC;&#x3002;&#x6B64;&#x5916;&#xFF0C;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;CMake&#x4F5C;&#x4E3A;&#x6784;&#x5EFA;&#x9879;&#x76EE;&#x7684;&#x5305;&#x7BA1;&#x7406;&#x5668;&#x3002;CMake&#x53EF;&#x4EE5;&#x641C;&#x7D22;&#x4F9D;&#x8D56;&#x9879;&#xFF0C;&#x5982;&#x679C;&#x5728;&#x7CFB;&#x7EDF;&#x4E0A;&#x627E;&#x4E0D;&#x5230;&#x4F9D;&#x8D56;&#x9879;&#xFF0C;&#x5219;&#x91CD;&#x65B0;&#x6784;&#x5EFA;&#x5B83;&#x4EEC;&#x3002;&#x8FD9;&#x91CC;&#x9700;&#x8981;&#x4E09;&#x4E2A;<code>CMakeLists.txt</code>&#x6587;&#x4EF6;&#xFF1A;</p>
<ul>
<li>&#x4E3B;<code>CMakeLists.txt</code>&#x6587;&#x4EF6;&#x5305;&#x542B;&#x9879;&#x76EE;&#x548C;&#x4F9D;&#x8D56;&#x9879;&#x5171;&#x4EAB;&#x7684;&#x8BBE;&#x7F6E;&#xFF0C;&#x8FD8;&#x5305;&#x62EC;&#x6211;&#x4EEC;&#x81EA;&#x5DF1;&#x7684;&#x9879;&#x76EE;(&#x4F5C;&#x4E3A;&#x5916;&#x90E8;&#x9879;&#x76EE;)&#x3002;&#x672C;&#x4F8B;&#x4E2D;&#xFF0C;&#x6211;&#x4EEC;&#x9009;&#x62E9;&#x7684;&#x540D;&#x79F0;&#x4E3A;<code>${PROJECT_NAME}_core</code>&#xFF1B;&#x4E5F;&#x5C31;&#x662F;<code>recipe-04_core</code>&#xFF0C;&#x56E0;&#x4E3A;&#x9879;&#x76EE;&#x540D;&#x79F0;<code>recipe-04</code>&#x7528;&#x4E8E;&#x8D85;&#x7EA7;&#x6784;&#x5EFA;&#x3002;</li>
<li>&#x5916;&#x90E8;<code>CMakeLists.txt</code>&#x6587;&#x4EF6;&#x5C06;&#x5C1D;&#x8BD5;&#x67E5;&#x627E;&#x4E0A;&#x6E38;&#x4F9D;&#x8D56;&#x9879;&#xFF0C;&#x5E76;&#x5728;&#x5BFC;&#x5165;&#x76EE;&#x6807;&#x548C;&#x6784;&#x5EFA;&#x76EE;&#x6807;&#x4E4B;&#x95F4;&#x8FDB;&#x884C;&#x5207;&#x6362;&#xFF0C;&#x8FD9;&#x53D6;&#x51B3;&#x4E8E;&#x662F;&#x5426;&#x627E;&#x5230;&#x4E86;&#x4F9D;&#x8D56;&#x9879;&#x3002;&#x5BF9;&#x4E8E;&#x6BCF;&#x4E2A;&#x4F9D;&#x8D56;&#x9879;&#xFF0C;&#x6700;&#x597D;&#x6709;&#x5355;&#x72EC;&#x7684;&#x5B50;&#x76EE;&#x5F55;&#xFF0C;&#x5176;&#x4E2D;&#x5305;&#x542B;&#x4E00;&#x4E2A;<code>CMakeLists.txt</code>&#x6587;&#x4EF6;&#x3002;</li>
<li>&#x6700;&#x540E;&#xFF0C;&#x6211;&#x4EEC;&#x9879;&#x76EE;&#x7684;<code>CMakeLists.txt</code>&#x6587;&#x4EF6;&#xFF0C;&#x53EF;&#x4EE5;&#x6784;&#x5EFA;&#x4E00;&#x4E2A;&#x72EC;&#x7ACB;&#x7684;CMake&#x9879;&#x76EE;&#x3002;&#x5728;&#x539F;&#x5219;&#x4E0A;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x81EA;&#x5DF1;&#x914D;&#x7F6E;&#x548C;&#x6784;&#x5EFA;&#x5B83;&#xFF0C;&#x800C;&#x4E0D;&#x9700;&#x8981;&#x8D85;&#x7EA7;&#x6784;&#x5EFA;&#x63D0;&#x4F9B;&#x7684;&#x4F9D;&#x8D56;&#x5173;&#x7CFB;&#x7BA1;&#x7406;&#x5DE5;&#x5177;&#x3002;</li>
</ul>
<p>&#x5F53;&#x5BF9;&#x6D88;&#x606F;&#x5E93;&#x7684;&#x4F9D;&#x8D56;&#x5173;&#x7CFB;&#x672A;&#x5F97;&#x5230;&#x6EE1;&#x8DB3;&#x65F6;&#xFF0C;&#x5C06;&#x9996;&#x5148;&#x8003;&#x8651;&#x8D85;&#x7EA7;&#x6784;&#x5EFA;:</p>
<pre><code class="lang-shell">$ mkdir -p build
$ cd build
$ cmake -DCMAKE_INSTALL_PREFIX=$HOME/Software/recipe-04 ..
</code></pre>
<p>&#x8BA9;CMake&#x67E5;&#x627E;&#x5E93;&#xFF0C;&#x8FD9;&#x662F;&#x6211;&#x4EEC;&#x5F97;&#x5230;&#x7684;&#x8F93;&#x51FA;:</p>
<pre><code class="lang-shell">-- The CXX compiler identification is GNU 7.3.0
-- Check for working CXX compiler: /nix/store/gqg2vrcq7krqi9rrl6pphvsg81sb8pjw-gcc-wrapper-7.3.0/bin/g++
-- Check for working CXX compiler: /nix/store/gqg2vrcq7krqi9rrl6pphvsg81sb8pjw-gcc-wrapper-7.3.0/bin/g++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Project will be installed to /home/roberto/Software/recipe-04
-- Build type set to Release
-- Installing LIB components to /home/roberto/Software/recipe-04/lib64
-- Installing BIN components to /home/roberto/Software/recipe-04/bin
-- Installing INCLUDE components to /home/roberto/Software/recipe-04/include
-- Installing CMAKE components to /home/roberto/Software/recipe-04/share/cmake/recipe-04
-- recipe-04 staged install: /home/roberto/Workspace/robertodr/cmake-cookbook/chapter-10/recipe-04/cxx-example/build/stage
-- Suitable message could not be located, Building message instead.
-- Configuring done
-- Generating done
-- Build files have been written to: /home/roberto/Workspace/robertodr/cmake-cookbook/chapter-10/recipe-04/cxx-example/build
</code></pre>
<p>&#x6839;&#x636E;&#x6307;&#x4EE4;&#xFF0C;CMake&#x62A5;&#x544A;&#x5982;&#x4E0B;:</p>
<ul>
<li>&#x5B89;&#x88C5;&#x5C06;&#x5206;&#x9636;&#x6BB5;&#x8FDB;&#x5165;&#x6784;&#x5EFA;&#x6811;&#x3002;&#x5206;&#x9636;&#x6BB5;&#x5B89;&#x88C5;&#x662F;&#x5BF9;&#x5B9E;&#x9645;&#x5B89;&#x88C5;&#x8FC7;&#x7A0B;&#x8FDB;&#x884C;&#x6C99;&#x7BB1;&#x5316;&#x7684;&#x4E00;&#x79CD;&#x65B9;&#x6CD5;&#x3002;&#x4F5C;&#x4E3A;&#x5F00;&#x53D1;&#x4EBA;&#x5458;&#xFF0C;&#x8FD9;&#x5BF9;&#x4E8E;&#x5728;&#x8FD0;&#x884C;&#x5B89;&#x88C5;&#x547D;&#x4EE4;&#x4E4B;&#x524D;&#x68C0;&#x67E5;&#x6240;&#x6709;&#x5E93;&#x3001;&#x53EF;&#x6267;&#x884C;&#x7A0B;&#x5E8F;&#x548C;&#x6587;&#x4EF6;&#x662F;&#x5426;&#x5B89;&#x88C5;&#x5728;&#x6B63;&#x786E;&#x7684;&#x4F4D;&#x7F6E;&#x975E;&#x5E38;&#x6709;&#x7528;&#x3002;&#x5BF9;&#x4E8E;&#x7528;&#x6237;&#x6765;&#x8BF4;&#xFF0C;&#x53EF;&#x5728;&#x6784;&#x5EFA;&#x76EE;&#x5F55;&#x4E2D;&#x7ED9;&#x51FA;&#x4E86;&#x76F8;&#x540C;&#x7684;&#x7ED3;&#x6784;&#x3002;&#x8FD9;&#x6837;&#xFF0C;&#x5373;&#x4F7F;&#x6CA1;&#x6709;&#x8FD0;&#x884C;&#x6B63;&#x786E;&#x7684;&#x5B89;&#x88C5;&#xFF0C;&#x6211;&#x4EEC;&#x7684;&#x9879;&#x76EE;&#x4E5F;&#x53EF;&#x4EE5;&#x7ACB;&#x5373;&#x4F7F;&#x7528;&#x3002;</li>
<li>&#x7CFB;&#x7EDF;&#x4E0A;&#x6CA1;&#x6709;&#x627E;&#x5230;&#x5408;&#x9002;&#x7684;&#x6D88;&#x606F;&#x5E93;&#x3002;&#x7136;&#x540E;&#xFF0C;CMake&#x5C06;&#x8FD0;&#x884C;&#x5728;&#x6784;&#x5EFA;&#x9879;&#x76EE;&#x4E4B;&#x524D;&#x6784;&#x5EFA;&#x5E93;&#x6240;&#x63D0;&#x4F9B;&#x7684;&#x547D;&#x4EE4;&#xFF0C;&#x4EE5;&#x6EE1;&#x8DB3;&#x8FD9;&#x79CD;&#x4F9D;&#x8D56;&#x6027;&#x3002;</li>
</ul>
<p>&#x5982;&#x679C;&#x5E93;&#x5DF2;&#x7ECF;&#x4F4D;&#x4E8E;&#x7CFB;&#x7EDF;&#x7684;&#x5DF2;&#x77E5;&#x4F4D;&#x7F6E;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x5C06;<code>-Dmessage_DIR</code>&#x9009;&#x9879;&#x4F20;&#x9012;&#x7ED9;CMake:</p>
<pre><code class="lang-shell">$ cmake -DCMAKE_INSTALL_PREFIX=$HOME/Software/use_message -Dmessage_DIR=$HOME/Software/message/share/cmake/message ..
</code></pre>
<p>&#x4E8B;&#x5B9E;&#x4E0A;&#xFF0C;&#x8FD9;&#x4E2A;&#x5E93;&#x5DF2;&#x7ECF;&#x627E;&#x5230;&#x5E76;&#x5BFC;&#x5165;&#x3002;&#x6211;&#x4EEC;&#x5BF9;&#x81EA;&#x5DF1;&#x7684;&#x9879;&#x76EE;&#x8FDB;&#x884C;&#x5EFA;&#x9020;&#x64CD;&#x4F5C;:</p>
<pre><code class="lang-shell">-- The CXX compiler identification is GNU 7.3.0
-- Check for working CXX compiler: /nix/store/gqg2vrcq7krqi9rrl6pphvsg81sb8pjw-gcc-wrapper-7.3.0/bin/g++
-- Check for working CXX compiler: /nix/store/gqg2vrcq7krqi9rrl6pphvsg81sb8pjw-gcc-wrapper-7.3.0/bin/g++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Project will be installed to /home/roberto/Software/recipe-04
-- Build type set to Release
-- Installing LIB components to /home/roberto/Software/recipe-04/lib64
-- Installing BIN components to /home/roberto/Software/recipe-04/bin
-- Installing INCLUDE components to /home/roberto/Software/recipe-04/include
-- Installing CMAKE components to /home/roberto/Software/recipe-04/share/cmake/recipe-04
-- recipe-04 staged install: /home/roberto/Workspace/robertodr/cmake-cookbook/chapter-10/recipe-04/cxx-example/build/stage
-- Checking for one of the modules &apos;uuid&apos;
-- Found message: /home/roberto/Software/message/lib64/libmessage.so.1 (found version 1.0.0)
-- Configuring done
-- Generating done
-- Build files have been written to: /home/roberto/Workspace/robertodr/cmake-cookbook/chapter-10/recipe-04/cxx-example/build
</code></pre>
<p>&#x9879;&#x76EE;&#x7684;&#x6700;&#x7EC8;&#x5B89;&#x88C5;&#x89C4;&#x5219;&#x662F;&#xFF0C;&#x5C06;&#x5B89;&#x88C5;&#x6587;&#x4EF6;&#x590D;&#x5236;&#x5230;<code>CMAKE_INSTALL_PREFIX</code>:</p>
<pre><code class="lang-cmake"><span class="hljs-keyword">install</span>(
  DIRECTORY
      <span class="hljs-variable">${STAGED_INSTALL_PREFIX}</span>/
  DESTINATION
      .
  USE_SOURCE_PERMISSIONS
  )
</code></pre>
<p>&#x6CE8;&#x610F;&#x4F7F;&#x7528;<code>.</code>&#x800C;&#x4E0D;&#x662F;&#x7EDD;&#x5BF9;&#x8DEF;&#x5F84;<code>${CMAKE_INSTALL_PREFIX}</code>&#xFF0C;&#x8FD9;&#x6837;CPack&#x5DE5;&#x5177;&#x5C31;&#x53EF;&#x4EE5;&#x6B63;&#x786E;&#x7406;&#x89E3;&#x8BE5;&#x89C4;&#x5219;&#x3002;CPack&#x7684;&#x7528;&#x6CD5;&#x5C06;&#x5728;&#x7B2C;11&#x7AE0;&#x4E2D;&#x4ECB;&#x7ECD;&#x3002;</p>
<p><code>recipe-04_core</code>&#x9879;&#x76EE;&#x6784;&#x5EFA;&#x4E00;&#x4E2A;&#x7B80;&#x5355;&#x7684;&#x53EF;&#x6267;&#x884C;&#x76EE;&#x6807;&#xFF0C;&#x8BE5;&#x76EE;&#x6807;&#x94FE;&#x63A5;&#x5230;&#x6D88;&#x606F;&#x52A8;&#x6001;&#x5E93;&#x3002;&#x6B63;&#x5982;&#x672C;&#x7AE0;&#x524D;&#x51E0;&#x8282;&#x6240;&#x8BA8;&#x8BBA;&#xFF0C;&#x4E3A;&#x4E86;&#x8BA9;&#x53EF;&#x6267;&#x884C;&#x6587;&#x4EF6;&#x6B63;&#x786E;&#x8FD0;&#x884C;&#xFF0C;&#x9700;&#x8981;&#x6B63;&#x786E;&#x8BBE;&#x7F6E;<code>RPATH</code>&#x3002;&#x672C;&#x7AE0;&#x7684;&#x7B2C;1&#x8282;&#x5C55;&#x793A;&#x4E86;&#xFF0C;&#x5982;&#x4F55;&#x5728;CMake&#x7684;&#x5E2E;&#x52A9;&#x4E0B;&#x5B9E;&#x73B0;&#x8FD9;&#x4E00;&#x70B9;&#xFF0C;&#x540C;&#x6837;&#x7684;&#x6A21;&#x5F0F;&#x5728;<code>CMakeLists.txt</code>&#x4E2D;&#x88AB;&#x91CD;&#x7528;&#xFF0C;&#x7528;&#x4E8E;&#x521B;&#x5EFA;<code>use_message</code>&#x7684;&#x53EF;&#x6267;&#x884C;&#x76EE;&#x6807;:</p>
<pre><code class="lang-cmake">file(RELATIVE_PATH _rel <span class="hljs-variable">${CMAKE_INSTALL_PREFIX}</span>/<span class="hljs-variable">${CMAKE_INSTALL_BINDIR}</span> <span class="hljs-variable">${CMAKE_INSTALL_PREFIX}</span>)
<span class="hljs-keyword">if</span>(APPLE)
  <span class="hljs-keyword">set</span>(_rpath <span class="hljs-string">&quot;@loader_path/${_rel}&quot;</span>)
<span class="hljs-keyword">else</span>()
  <span class="hljs-keyword">set</span>(_rpath <span class="hljs-string">&quot;\$ORIGIN/${_rel}&quot;</span>)
<span class="hljs-keyword">endif</span>()
file(TO_NATIVE_PATH <span class="hljs-string">&quot;${_rpath}/${CMAKE_INSTALL_LIBDIR}&quot;</span> use_message_RPATH)

<span class="hljs-keyword">set_target_properties</span>(use_message
  PROPERTIES
    MACOSX_RPATH <span class="hljs-keyword">ON</span>
    SKIP_BUILD_RPATH <span class="hljs-keyword">OFF</span>
    BUILD_WITH_INSTALL_RPATH <span class="hljs-keyword">OFF</span>
    INSTALL_RPATH <span class="hljs-string">&quot;${use_message_RPATH}&quot;</span>
    INSTALL_RPATH_USE_LINK_PATH <span class="hljs-keyword">ON</span>
  )
</code></pre>
<p>&#x4E3A;&#x4E86;&#x68C0;&#x67E5;&#x8FD9;&#x662F;&#x5426;&#x5408;&#x9002;&#xFF0C;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x672C;&#x673A;&#x5DE5;&#x5177;&#x6253;&#x5370;&#x5DF2;&#x5B89;&#x88C5;&#x7684;&#x53EF;&#x6267;&#x884C;&#x6587;&#x4EF6;&#x7684;<code>RPATH</code>&#x3002;&#x6211;&#x4EEC;&#x5C06;&#x5BF9;&#x8BE5;&#x5DE5;&#x5177;&#x7684;&#x8C03;&#x7528;&#xFF0C;&#x5C01;&#x88C5;&#x5230;Python&#x811A;&#x672C;&#x4E2D;&#xFF0C;&#x5E76;&#x5C06;&#x5176;&#x8FDB;&#x4E00;&#x6B65;&#x5C01;&#x88C5;&#x5230;CMake&#x811A;&#x672C;&#x4E2D;&#x3002;&#x6700;&#x540E;&#xFF0C;&#x4F7F;&#x7528;<code>SCRIPT</code>&#x5173;&#x952E;&#x5B57;&#x5C06;CMake&#x811A;&#x672C;&#x4F5C;&#x4E3A;&#x5B89;&#x88C5;&#x89C4;&#x5219;&#x8C03;&#x7528;:</p>
<pre><code class="lang-cmake"><span class="hljs-keyword">if</span>(UNIX)
  <span class="hljs-keyword">set</span>(PRINT_SCRIPT <span class="hljs-string">&quot;${CMAKE_CURRENT_LIST_DIR}/cmake/print_rpath.py&quot;</span>)
  <span class="hljs-keyword">configure_file</span>(cmake/install_hook.cmake.in install_hook.cmake @ONLY)
  <span class="hljs-keyword">install</span>(
    SCRIPT
      <span class="hljs-variable">${CMAKE_CURRENT_BINARY_DIR}</span>/install_hook.cmake
    )
<span class="hljs-keyword">endif</span>()
</code></pre>
<p>&#x811A;&#x672C;&#x662F;&#x5728;&#x5B89;&#x88C5;&#x6700;&#x540E;&#x8FDB;&#x884C;&#x6267;&#x884C;:</p>
<pre><code class="lang-shell">$ cmake --build build --target install
</code></pre>
<p>GNU/Linux&#x7CFB;&#x7EDF;&#x4E0A;&#xFF0C;&#x6211;&#x4EEC;&#x5C06;&#x770B;&#x5230;&#x4EE5;&#x4E0B;&#x8F93;&#x51FA;:</p>
<pre><code class="lang-shell">Install the project...
-- Install configuration: &quot;Release&quot;
-- Installing: /home/roberto/Software/recipe-04/.
-- Installing: /home/roberto/Software/recipe-04/./lib64
-- Installing: /home/roberto/Software/recipe-04/./lib64/libmessage.so
-- Installing: /home/roberto/Software/recipe-04/./lib64/libmessage_s.a
-- Installing: /home/roberto/Software/recipe-04/./lib64/libmessage.so.1
-- Installing: /home/roberto/Software/recipe-04/./include
-- Installing: /home/roberto/Software/recipe-04/./include/message
-- Installing: /home/roberto/Software/recipe-04/./include/message/Message.hpp
-- Installing: /home/roberto/Software/recipe-04/./include/message/messageExport.h
-- Installing: /home/roberto/Software/recipe-04/./share
-- Installing: /home/roberto/Software/recipe-04/./share/cmake
-- Installing: /home/roberto/Software/recipe-04/./share/cmake/message
-- Installing: /home/roberto/Software/recipe-04/./share/cmake/message/messageTargets-release.cmake
-- Installing: /home/roberto/Software/recipe-04/./share/cmake/message/messageConfigVersion.cmake
-- Installing: /home/roberto/Software/recipe-04/./share/cmake/message/messageConfig.cmake
-- Installing: /home/roberto/Software/recipe-04/./share/cmake/message/messageTargets.cmake
-- Installing: /home/roberto/Software/recipe-04/./bin
-- Installing: /home/roberto/Software/recipe-04/./bin/hello-world_wAR
-- Installing: /home/roberto/Software/recipe-04/./bin/use_message
-- Installing: /home/roberto/Software/recipe-04/./bin/hello-world_wDSO
-- ELF patching tool chrpath FOUND
-- RPATH for /home/roberto/Software/recipe-04/bin/use_message is /home/roberto/Software/recipe-04/bin/use_message: RUNPATH=$ORIGIN/../lib64:/home/roberto/Workspace/robertodr/cmake-cookbook/chapter-10/recipe-04/cxx-example/build/stage/lib64:/nix/store/di389pfcw2krnmh8nmkn55d1rnzmba37-CMake-Cookbook/lib64:/nix/store/di389pfcw2krnmh8nmkn55d1rnzmba37-CMake-Cookbook/lib:/nix/store/mjs2b8mmid86lvbzibzdlz8w5yrjgcnf-util-linux-2.31.1/lib:/nix/store/2kcrj1ksd2a14bm5sky182fv2xwfhfap-glibc-2.26-131/lib:/nix/store/4zd34747fz0ggzzasy4icgn3lmy89pra-gcc-7.3.0-lib/lib
-- Running /home/roberto/Software/recipe-04/bin/use_message:
This is my very nice message:
Hello, World! From a client of yours!
...and here is its UUID: a8014bf7-5dfa-45e2-8408-12e9a5941825
This is my very nice message:
Goodbye, World! From a client of yours!
...and here is its UUID: ac971ef4-7606-460f-9144-1ad96f713647
</code></pre>
<p><strong>NOTE</strong>:<em>&#x6211;&#x4EEC;&#x5EFA;&#x8BAE;&#x4F7F;&#x7528;&#x7684;&#x5DE5;&#x5177;&#x662F;PatchELF (<a href="https://nixos.org/patchelf.html" target="_blank">https://nixos.org/patchelf.html</a> )&#x3001;chrpath (<a href="https://linux.die.net/man/1/chrpath" target="_blank">https://linux.die.net/man/1/chrpath</a> )&#x548C;otool (<a href="http://www.manpagez.com/man/1/otool/" target="_blank">http://www.manpagez.com/man/1/otool/</a> )&#x3002;&#x7B2C;&#x4E00;&#x79CD;&#x65B9;&#x6CD5;&#x9002;&#x7528;&#x4E8E;GNU/Linux&#x548C;macOS&#xFF0C;&#x800C;chrpath&#x548C;otool&#x5206;&#x522B;&#x9002;&#x7528;&#x4E8E;GNU/Linux&#x548C;macOS&#x3002;</em></p>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="10.3-chinese.html" class="navigation navigation-prev " aria-label="Previous page: 10.3 输出目标">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="../chapter11/11.0-chinese.html" class="navigation navigation-next " aria-label="Next page: 第11章 打包项目">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"10.4 安装超级构建","level":"1.13.4","depth":2,"next":{"title":"第11章 打包项目","level":"1.14","depth":1,"path":"content/chapter11/11.0-chinese.md","ref":"content/chapter11/11.0-chinese.md","articles":[{"title":"11.1 生成源代码和二进制包","level":"1.14.1","depth":2,"path":"content/chapter11/11.1-chinese.md","ref":"content/chapter11/11.1-chinese.md","articles":[]},{"title":"11.2 通过PyPI发布使用CMake/pybind11构建的C++/Python项目","level":"1.14.2","depth":2,"path":"content/chapter11/11.2-chinese.md","ref":"content/chapter11/11.2-chinese.md","articles":[]},{"title":"11.3 通过PyPI发布使用CMake/CFFI构建C/Fortran/Python项目","level":"1.14.3","depth":2,"path":"content/chapter11/11.3-chinese.md","ref":"content/chapter11/11.3-chinese.md","articles":[]},{"title":"11.4 以Conda包的形式发布一个简单的项目","level":"1.14.4","depth":2,"path":"content/chapter11/11.4-chinese.md","ref":"content/chapter11/11.4-chinese.md","articles":[]},{"title":"11.5 将Conda包作为依赖项发布给项目","level":"1.14.5","depth":2,"path":"content/chapter11/11.5-chinese.md","ref":"content/chapter11/11.5-chinese.md","articles":[]}]},"previous":{"title":"10.3 输出目标","level":"1.13.3","depth":2,"path":"content/chapter10/10.3-chinese.md","ref":"content/chapter10/10.3-chinese.md","articles":[]},"dir":"ltr"},"config":{"plugins":["-lunr","-search","search-pro","-sharing","github","pageview-count"],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"github":{"url":"https://github.com/arcticfox1919"},"search-pro":{},"pageview-count":{},"highlight":{},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"theme":"default","author":"陈晓伟 chenxiaowei","pdf":{"pageNumbers":false,"fontSize":30,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":30,"left":30,"top":30,"bottom":50}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"《CMake Cookbook》中文版","language":"zh-hans","gitbook":"*","description":"作为对《CMake Cookbook》英文版的中文翻译。","direction":"ltr"},"file":{"path":"content/chapter10/10.4-chinese.md","mtime":"2020-09-22T10:39:01.895Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2020-09-22T11:21:21.825Z"},"basePath":"../..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../../gitbook/gitbook.js"></script>
    <script src="../../gitbook/theme.js"></script>
    
        
        <script src="../../gitbook/gitbook-plugin-search-pro/jquery.mark.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search-pro/search.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-github/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-pageview-count/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

